- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using System.IO;
- namespace xmlTojson
- {
- public partial class DBForm : Form
- {
- public DBForm()
- {
- InitializeComponent();
- }
- #region 数据库操作
- //查询
- //SELECT [DLLID],[DLLCONTENT],[DLLVERTION],[DLLBACKUPS] FROM [TestDB].[dbo].[My_Table]
- //更新
- //go
- //UPDATE [TestDB].[dbo].[My_Table] SET [DLLBACKUPS]=[DLLCONTENT],[DLLCONTENT]=‘789‘ WHERE [DLLID]=‘20160602‘
- //插入
- //go
- //INSERT INTO [TestDB].[dbo].[My_Table] VALUES(‘20180602‘,‘qqq‘,‘222‘,‘444‘)
- //删除
- //go
- //DELETE FROM [TestDB].[dbo].[My_Table] WHERE [DLLID]=‘20170602‘
- #endregion
- private void btnSave_Click(object sender, EventArgs e)
- {
- byte[] byteBinary = File.ReadAllBytes(@"E:\Programme\MyWork\WLJTools\xmlTojson\xmlTojson\Lib\3w.dll");
- //string saveString = Encoding.UTF8.GetString(byteBinary);
- #region DataBase UPdate
- //"Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=Northwind;");
- string connectstring = "Data Source=PC-20151213LNGQ;Integrated Security=True;Initial Catalog=TestDB";
- string sqlCommandText = "UPDATE My_Table SET [DLLBACKUPS]=[DLLCONTENT],[DLLCONTENT]=" + "@DLLCONTENT" + " WHERE [DLLID]=‘100310125‘";
- SqlConnection sqlConn = new SqlConnection(connectstring);
- sqlConn.Open();
- SqlCommand sqlCommand = sqlConn.CreateCommand();
- SqlTransaction transact = sqlConn.BeginTransaction();
- //创建参数
- SqlParameter para = new SqlParameter("@DLLCONTENT", SqlDbType.VarBinary, byteBinary.Length, ParameterDirection.Input, true, 0, 0, "DLLCONTENT", DataRowVersion.Default, byteBinary);
- sqlCommand.Parameters.Add(para);
- //sqlCommand.Parameters["@DLLCONTENT"] = byteBinary;
- //sqlCommand.Parameters.Add(
- sqlCommand.Connection = sqlConn;
- sqlCommand.CommandText = sqlCommandText;
- sqlCommand.Transaction = transact;
- try
- {
- int xx = (int)sqlCommand.ExecuteNonQuery();
- if (xx > 0)
- {
- MessageBox.Show("Commit Sucess!", "提示信息:");
- }
- //return;
- //SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
- //if (reader.Read())
- //{
- // byte[] result = (byte[])reader.GetValue(1);
- //}
- transact.Commit();
- MessageBox.Show("Commit Sucess!", "提示信息:");
- }
- catch (Exception ex)
- {
- MessageBox.Show("Commit Exception Type: {0}" + ex.GetType(), "提示信息:");
- try
- {
- transact.Rollback();
- }
- catch (Exception ex2)
- {
- MessageBox.Show("Rollback Exception Type: {0}" + ex2.GetType(), "提示信息:");
- }
- }
- #endregion
- }
- private void btnRead_Click(object sender, EventArgs e)
- {
- //"Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=Northwind;");
- string connectstring = "Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=TestDB";
- string sqlCommandText = "SELECT [DLLID],[DLLCONTENT],[DLLVERTION],[DLLBACKUPS] FROM [TestDB].[dbo].[My_Table] WHERE [DLLID]=‘100310125‘";
- SqlConnection sqlConn = new SqlConnection(connectstring);
- sqlConn.Open();
- SqlCommand sqlCommand = sqlConn.CreateCommand();
- SqlTransaction transact = sqlConn.BeginTransaction();
- sqlCommand.CommandText = sqlCommandText;
- sqlCommand.Transaction = transact;
- try
- {
- SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
- if (reader.Read())
- {
- byte[] result = (byte[])reader.GetValue(1);
- File.WriteAllBytes(@"E:\Programme\MyWork\WLJTools\xmlTojson\xmlTojson\Lib\www.dll", result);
- }
- reader.Close();
- MessageBox.Show("Commit Sucess!", "提示信息:");
- }
- catch (Exception ex)
- {
- MessageBox.Show("Commit Exception Type: {0}" + ex.GetType(), "提示信息:");
- }
- }
- }
- }
时间: 2024-10-30 14:27:47