码迷,mamicode.com
首页 > 数据库 > 详细

数据库操作之简单带参操作

时间:2016-06-04 11:48:46      阅读:279      评论: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.Data.SqlClient;
  10. using System.IO;
  11. namespace xmlTojson
  12. {
  13. public partial class DBForm : Form
  14. {
  15. public DBForm()
  16. {
  17. InitializeComponent();
  18. }
  19. #region 数据库操作
  20. //查询
  21. //SELECT [DLLID],[DLLCONTENT],[DLLVERTION],[DLLBACKUPS] FROM [TestDB].[dbo].[My_Table]
  22. //更新
  23. //go
  24. //UPDATE [TestDB].[dbo].[My_Table] SET [DLLBACKUPS]=[DLLCONTENT],[DLLCONTENT]=‘789‘ WHERE [DLLID]=‘20160602‘
  25. //插入
  26. //go
  27. //INSERT INTO [TestDB].[dbo].[My_Table] VALUES(‘20180602‘,‘qqq‘,‘222‘,‘444‘)
  28. //删除
  29. //go
  30. //DELETE FROM [TestDB].[dbo].[My_Table] WHERE [DLLID]=‘20170602‘
  31. #endregion
  32. private void btnSave_Click(object sender, EventArgs e)
  33. {
  34. byte[] byteBinary = File.ReadAllBytes(@"E:\Programme\MyWork\WLJTools\xmlTojson\xmlTojson\Lib\3w.dll");
  35. //string saveString = Encoding.UTF8.GetString(byteBinary);
  36. #region DataBase UPdate
  37. //"Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=Northwind;");
  38. string connectstring = "Data Source=PC-20151213LNGQ;Integrated Security=True;Initial Catalog=TestDB";
  39. string sqlCommandText = "UPDATE My_Table SET [DLLBACKUPS]=[DLLCONTENT],[DLLCONTENT]=" + "@DLLCONTENT" + " WHERE [DLLID]=‘100310125‘";
  40. SqlConnection sqlConn = new SqlConnection(connectstring);
  41. sqlConn.Open();
  42. SqlCommand sqlCommand = sqlConn.CreateCommand();
  43. SqlTransaction transact = sqlConn.BeginTransaction();
  44. //创建参数
  45. SqlParameter para = new SqlParameter("@DLLCONTENT", SqlDbType.VarBinary, byteBinary.Length, ParameterDirection.Input, true, 0, 0, "DLLCONTENT", DataRowVersion.Default, byteBinary);
  46. sqlCommand.Parameters.Add(para);
  47. //sqlCommand.Parameters["@DLLCONTENT"] = byteBinary;
  48. //sqlCommand.Parameters.Add(
  49. sqlCommand.Connection = sqlConn;
  50. sqlCommand.CommandText = sqlCommandText;
  51. sqlCommand.Transaction = transact;
  52. try
  53. {
  54. int xx = (int)sqlCommand.ExecuteNonQuery();
  55. if (xx > 0)
  56. {
  57. MessageBox.Show("Commit Sucess!", "提示信息:");
  58. }
  59. //return;
  60. //SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
  61. //if (reader.Read())
  62. //{
  63. // byte[] result = (byte[])reader.GetValue(1);
  64. //}
  65. transact.Commit();
  66. MessageBox.Show("Commit Sucess!", "提示信息:");
  67. }
  68. catch (Exception ex)
  69. {
  70. MessageBox.Show("Commit Exception Type: {0}" + ex.GetType(), "提示信息:");
  71. try
  72. {
  73. transact.Rollback();
  74. }
  75. catch (Exception ex2)
  76. {
  77. MessageBox.Show("Rollback Exception Type: {0}" + ex2.GetType(), "提示信息:");
  78. }
  79. }
  80. #endregion
  81. }
  82. private void btnRead_Click(object sender, EventArgs e)
  83. {
  84. //"Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=Northwind;");
  85. string connectstring = "Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=TestDB";
  86. string sqlCommandText = "SELECT [DLLID],[DLLCONTENT],[DLLVERTION],[DLLBACKUPS] FROM [TestDB].[dbo].[My_Table] WHERE [DLLID]=‘100310125‘";
  87. SqlConnection sqlConn = new SqlConnection(connectstring);
  88. sqlConn.Open();
  89. SqlCommand sqlCommand = sqlConn.CreateCommand();
  90. SqlTransaction transact = sqlConn.BeginTransaction();
  91. sqlCommand.CommandText = sqlCommandText;
  92. sqlCommand.Transaction = transact;
  93. try
  94. {
  95. SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
  96. if (reader.Read())
  97. {
  98. byte[] result = (byte[])reader.GetValue(1);
  99. File.WriteAllBytes(@"E:\Programme\MyWork\WLJTools\xmlTojson\xmlTojson\Lib\www.dll", result);
  100. }
  101. reader.Close();
  102. MessageBox.Show("Commit Sucess!", "提示信息:");
  103. }
  104. catch (Exception ex)
  105. {
  106. MessageBox.Show("Commit Exception Type: {0}" + ex.GetType(), "提示信息:");
  107. }
  108. }
  109. }
  110. }

数据库操作之简单带参操作

标签:

原文地址:http://www.cnblogs.com/1175429393wljblog/p/5558302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!