操作SQL Server数据源

1、查询记录

(1) 把连接字符窜修改成:

 string strcon =@"server=服务器名;database=库名;uid=用户名;pwd =口令“;

(2)把命名空间修改成:System.Data.SqlClient。

(3)把类OleDbConnection、OleDbCommand、OleDbDataReader修改成:

SqlConnection、SqlCommand、SqlDataReader




2、插入记录

使用Command对象的ExecuteNonQuery()方法来实现插入数据操作,用SQL的insert语句来设定具体的要插入的新的记录内容。



3、修改记录

update  StudentDB.student.set Name='xxx' where ID=3


4、删除记录

myCommand.CommandText="delete StudentDB..student where ID=3"


5、执行存储过程

利用存储过程,我们可以把多条SQL语句组合到一起执行。

USE StudentDB

GO 


Create PROCEDURE StoreProcl(@ID int)

as  

begin



end


6、有返回值的存储过程


运行以上代码,结果可能并不是你预期的,因为ID字段的值是系统给定的,我们可以让存储过程返回新插入的ID值。

在C#程序中执行上面的存储过程,利用SqlParameter的对象,即可获得返回参数。

原文地址:https://www.cnblogs.com/Anzhongliu/p/6091853.html