-
数据库操作之简单带参操作
- 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:ProgrammeMyWorkWLJToolsxmlTojsonxmlTojsonLib3w.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:ProgrammeMyWorkWLJToolsxmlTojsonxmlTojsonLibwww.dll", result);
- }
- reader.Close();
- MessageBox.Show("Commit Sucess!", "提示信息:");
- }
- catch (Exception ex)
- {
- MessageBox.Show("Commit Exception Type: {0}" + ex.GetType(), "提示信息:");
- }
- }
- }
- }
-
相关阅读:
mysql-connector-java(6.0以上)的时差问题
Mysql中的Date转换
Intellij idea 告警:'while' statement cannot complete without throwing an exception
JavaScript数组的操作
Intellij idea 告警:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
【网络文摘】2016年里做前端是怎样一种体验
【JavaScript Demo】回到顶部功能实现
【JavaScript 插件】实现图片倒影效果
【读书笔记《Bootstrap 实战》】6.单页营销网站
【读书笔记《Bootstrap 实战》】5.电子商务网站
-
原文地址:https://www.cnblogs.com/1175429393wljblog/p/5558302.html
Copyright © 2020-2023
润新知