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(), "提示信息:");
}
}
}
}
龙腾一族至尊龙骑
数据库操作之简单带参操作
原创
©著作权归作者所有:来自51CTO博客作者龙腾一族至尊龙骑的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:为Windows 7添加“Internet打印”功能
下一篇:奋进。。。。。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
MySQL数据库之简单表操作
有关MySQL简单表操作
数据库 MySQL mysql 字段 SQL -
【Qt之数据库操作】
【Qt之数据库操作】
数据库 qt jvm SQL 更新数据 -
mysql数据库很简单操作
进入linux系统root >/usr/bin/mysql -u rootmysql>show databases; #查看数据库mysql>use testtable; &n
数据库 create tables primary databases -
RUBY实践—数据库简单操作
开发环境:Ruby:1.9.1Rails:2.3.5Rake:0.8.7Rack:1.0.1Mysql:5.0.9Ruby-mysql:mysql-2.8.1-x86-mswinIDE:RubyMine2.0.1 数据库准备:datab:crystal 一、创建Ruby
数据库 ruby activerecord rubymine database