Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Configuration;
  10. using System.Data.SqlClient;
  11.  
  12. namespace Sample02
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. private void btnHyouji_Click(object sender, EventArgs e)
  22. {
  23. string connectionString = ConfigurationManager.ConnectionStrings["Db"].ConnectionString;
  24. using (SqlConnection connection = new SqlConnection(connectionString))
  25. {
  26. string sql = "select * from syohin where syohincode = @syohincode";
  27.  
  28. SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
  29.  
  30. adapter.SelectCommand.Parameters.Add("@syohincode", SqlDbType.Char);
  31. adapter.SelectCommand.Parameters["@syohincode"].Value = txtSyohinCode.Text;
  32.  
  33. DataSet dataSet = new DataSet();
  34. int cnt = adapter.Fill(dataSet, "syohin");
  35.  
  36. if (cnt != 0)
  37. {
  38. DataTable dataTable = dataSet.Tables["syohin"];
  39. txtSyohinName.Text = dataTable.Rows[0]["syohinname"].ToString();
  40. txtPrice.Text = ((int)dataTable.Rows[0]["price"]).ToString("c");
  41. txtBunrui.Text = dataTable.Rows[0]["bunruino"].ToString();
  42. }
  43. else
  44. {
  45. txtSyohinName.Text = "";
  46. txtPrice.Text = "";
  47. txtBunrui.Text = "";
  48. MessageBox.Show("該当商品なし", "検索エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
  49. }
  50. }
  51.  
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement