张涛 发表于 2011-12-30 13:23
直接用sql help 执行存储过程,
我的意思是:在.net中命令行不用SQL文本字符串,而是使用一个存储过程;我前天看到存储过程有好多好处:效率解释执行的SQL语句效率快,降低了客户和服务器之间的通信量等;我就想试试(下面是检索一个元组中的一个分量值!)
比如:
存储过程:
create procedure GetStudentName1
@name nvarchar(50)
as
select *from person where @name=name
go
C# Code:- //检索记录操作
- String connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
- using (SqlConnection conn = new SqlConnection(connstr))
- {
- conn.Open();
- using (SqlCommand cmd = new SqlCommand("GetStudentName1", conn))
- {
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@name", "丽丽");
- object i = cmd.ExecuteScalar();
- Console.WriteLine(i);
- }
- }
复制代码 可是我不知道怎么能把下面的ExcuteXmlReader()通过存储过程运行!- String connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
- using (SqlConnection conn = new SqlConnection(connstr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = "select * " + "from [person] for XML AUTO";
- XmlReader xr = cmd.ExecuteXmlReader();
- xr.Read();
- string data;
- do
- {
- data = xr.ReadOuterXml();
- if (!string.IsNullOrEmpty(data))
- Console.WriteLine(data);
- }
- while (!string.IsNullOrEmpty(data));
- conn.Close();
- }
- }
复制代码 |