数据库操作之简单带参操作
数据库 操作 简单
2023-09-14 08:57:07 时间
- 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(), "提示信息:");
- }
- }
- }
- }
相关文章
- mongodb数据库入门之CURD简单操作示例
- postgresql数据库安装及简单操作详解数据库
- MySQL异步查询:加速数据库操作(mysql异步查询)
- Mysql数据库备份的简单操作指南(mysql数据库怎么备份)
- 使用Java操作Redis数据库(java中使用redis)
- 使用DOS操作MySQL数据库(dos操作mysql)
- MySQL数据库简易教程:掌握简单操作(mysql数据教程)
- Linux系统下数据库备份的简单操作(linux数据库备份命令)
- MSSQL中附加数据库的简单操作(mssql附加数据库)
- 解决MySQL卸载问题:简单易懂操作指南(mysql数据库怎么卸载)
- 如何使用MySQL进行数据库克隆操作?(mysql克隆数据库)
- 易语言MSSQL开发例程:实现数据库操作的简单之道(易语言mssql例程)
- 的MySQL:让数据库操作更简单(mysql以开头)
- SQLServer 数据库操作技巧:5个实用的提示(sqlserver 提示)
- Oracle数据库修改操作:有效提升工作效率(修改oracle)
- 从远程连接MSSQL数据库——简单操作指南(连接远程mssql命令)
- C语言连接Oracle数据库字符串操作技巧(c连接oracle字符串)
- C语言调用Oracle数据库快速实现操作指令(c 调用oracle包体)
- 使用cmd快速操作Oracle数据库(cmd oracle操作)
- Oracle数据库操作中使用URL访问的简单指南(oracle使用url)
- Mysql数据库操作指南深入了解不同的操作技巧(mysql不同数据库操作)