📄 ado.txt
字号:
some CODES : ----080325AM
(我写这些代码的目的不是纯为了实验时候需要,而是相应的学习,刚刚在VS2005中写了一段,发现一些问题,
我学的ADO.NET 1.1版本的知识类现在都被VS2005版封装得更隐蔽了,但是这些代码还是很有用的,只有理解了这些,才可以在更高版本的IDE中掌控自如.现在都有VS2008出来了,但我还是要学2005的,因为英文水平不够,
一方面也得给自己鼓气,认真学英文!而对于这些代码,我是专门从一门好书上摘录下来的,应该是我所接受的...)
1.1
private void SQLConectionComponent()
{ //使用VS对象sqlConnection进行连接
sqlConnection1.connectionString="SERVER;"+"INTERGATED SECURITY=Ture"; //下划线表示连接
使用的是账号身份验证
try
{ //Try-Catch循环使您应用程序能捕获任何异常,并用预先定义好的方式处理
sqlConnection1.Open();
}
catch(Exception ex) //ex为Exception对象一个简单实例
{
MessageBox.Show("Connection error::"+ex.Tostrign());
}
sqlConnection1.Close();
} //可视化连接组件必须添加导入命名空间的指令,--"Using System.Data.SqlClient;"
这样就可以简化一些对象命令
1.2
private void SQLConnectionString(string sServer,string sUser,string sPwd)
{
SqlConnection cn = new sqlConnection();
cn.ConnectionString="SERVER="+sServer+";"+UID="+sUser+";PWD+sPwd;
try
{
cn.Open();
}
catch(Exception ex)
{
MessageBox.Show("Connection error::"+ex.Tostrign());
}
cn.Close();
private void SQLConnectionSSPI(string sServer)
{//打开信任连接
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture");
try
{
cn.Open();
}
catch(Exception ex)
{
MessageBox.Show("Connection error::"+ex.Tostrign());
}
cn.Close();
}
1.3 连接池使用,可以打开多个连接,在上面代码中多新建一个对象实例cn2即可;
2.1
private void SQLConectionComponent(string sServer, string sDB)
{
sqlConnection1.connectionString="SERVER;"+"INTERGATED SECURITY=Ture";DATABASE="+sDB;
sqlCommand1.CommandText="select * from customers";
//set the active connection
sqlCommand1.connection=sqlConnection1;
try
{
sqlConnection1.Open();
System.Data.SqlClient.SqlDataReader dr = sqlCommand1.ExecuteReader();
//若添加命名空间则此代码可以简写!;
这里创建SqlDataReader对象dr,SqlDataReader命令会从目标数据源中返回一个快速的只向前的数据流.
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
sqlConnection1.Close();
}
2.2
private void SQLConectionComponent(string sServer, string sDB)
{
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture";DATABASE="+sDB);
string sSQL;
SqlCommand cmd = new SqlCommand(" ",cn); //这里带有两个参数.第一个为字符串,初始为空;第二个为 SqlConnection对象.它与SQLConectionComponent(string sServer, string sDB)中的两个参数要匹配!
try
{
cn.Open();
sSQL="IF EXISTS"+"(SELECT * FROM dbo.Sysobjects"+"where id = object_id(N'[Department]')"
+"AND objectproperty (id,N'IsUserTable')=1"+"DROP Table [Department]";
cmd.ExcuteNoQuery();
// After drop the table;then create the table
sSQL="CREATE Table Cepartment"+"(DepartmentID Int NOT NULL,"+"DepartmentName char(25),"
+"Primary key (DepartmentID))";
cmd.CommandText=sSQL;
cmd.ExecuteNoQuery(); //此方法用于在联机的数据源上执行SQL语句,它用于DDL语句和动作查询,
如Insert,Update,Delete等.该方法返回受影响的行数,而不返回输出参数或结果集.
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
cn.Close(); // 关闭连接
}
2.3 执行参数化的SQL语句
private void SQLConectionComponent(string sServer, string sDB)
{
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture";DATABASE="+sDB);
//set up the command object's parpmeter types
SqlCommand cmd = new SqlCommand("INSERT INTO Department VALUES"+"(@DepartmentID,@DepartmentName)",cn);
//以上这句的参数也要和SQLConectionComponent(string sServer, string sDB)匹配!
SqlParameter parmDepartmentID = new SqlParameter("@DepartmentID",SqlDbType.Int);
parmDepartmentID.Direction=ParameterDirection.Input;//参数为输出类型.Input为SqlParameterDirection的枚举值
SqlParameter parmDepartmentName = new SqlParameter("@DepartmentName",SqlDbType.Char,25);
parmDepartmentName.Direction=ParameterDirection.Input;
//Add the parameter object to the command parameter's collection.
cmd.Parameters.Add(parmDepartmentID);
cmd.Parameters.Add(parmDepartmentName);
try
{//open...& prepare the command.
cn.Open();
cmd.Preapare();
//execute the prepared SQL statement to insert 10 rows
for(int i=1;i<=10;i++)
{
parmDepartmentID.Value = i;
parmDepartmentName.Value = "New Department" + i;
cmd.ExecuteNoQuery();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
cn.Close();
}
说明:1)注意在SQL中使用的参数标记的格式;
参数标记用于SQL中可替换的字符,运行时,这些参数会被Sqlcommand对象的Parameter机和所提供的值替换.
2)ADO或OleDbCommand 对象使用问号(?)来表示可替换的字符,而Sqlcommand对象要求所有的参数标记都已@字符开头.
2.4 执行带有返回值的存储过程
/这里补充一点,我对数据库中的存储过程的概念还使模糊的,什么时间还得研究研究...
/这是我临时找到的一篇不错的文章,是解说存储过程和触发器的...http://www.yscode.com/article/database/303.html
先在Query Analyzer中执行以下代码(用于创建存储过程,创建添加到Northwind中的StockValue存储过程)
CREATE PROCEDURE StockVaule
@ProductID int
AS
DECLARE @StockVaule money
SELECT StockVaule =(Units InStock * UnitPrice)
FROM Products where ProductID = @ProductID
RETURN @StockVaule
private void SQLCommandPSScalar(string sServer, string sDB)
{
SqlConnection cn = new sqlConnection("SERVER="+sServer+";INTERGATED SECURITY=Ture";DATABASE="+sDB);
//create the command object and set the SQL statment
SqlCommand cmd = new SqlCommand("StockVaule",cn);
cmd.CommandeType=CommandeType.StoredProedure; //属性StoredProedure指定Command对象的参数为存储过程
//create the parameter
cmd.Parameters.Add("@ProductID",SqlDbType.Int);
cmd.Parameters["@ProductID"].Direction=ParameterDirection.Input;
cmd.Parameters["@ProductID"].Vaule=1; //值1将被传递给存储过程
try
{
decimal nStockVaule;
cn.Open();
nStockVaule=(decimal)cmd.ExecuteScalar(); //执行存储过程
txtMidText=nStockVaule.Tostring();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
cn.Close(); // 关闭连接
}
说明:StockVaule为存储过程名称;
CommandeType属性有三个,StoredProedure--该命令为存储过程;TableDirect---为数据库表名称;Text---为SQL语句.
ExecuteScalar 方法用于执行返回单个标量值的存储过程或SQL语句,并把结果集的第一行上第一列返回给调用程序,而忽略返回值.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -