zoukankan      html  css  js  c++  java
  • 数据库操作之简单带参操作

    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:ProgrammeMyWorkWLJToolsxmlTojsonxmlTojsonLib3w.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:ProgrammeMyWorkWLJToolsxmlTojsonxmlTojsonLibwww.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. }
  • 相关阅读:
    fms中使用的页签(使用的模板)
    layui—layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案(转)
    mybatis--oracle 利用接口 实现简单的增删改查以及一对一的两表关联查询
    mybatis--oracle 利用配置 实现简单的增删改查
    Java DecimalFormat的主要功能及使用方法
    SpringMVC的@ResponseBody返回字符串乱码问题解决
    org.apache.commons.lang.exception.NestableRuntimeException等缺少jar包的解决办法
    Mybatis框架搭建和简单的增删改查
    Struts2上传
    Struts2转换器
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/5558302.html
Copyright © 2011-2022 走看看