ADO.NET(三)调用存储过程
2008-03-28 14:07
399 查看
用一个命令对象调用存储过程,就是定义存储过程的名称,给过程的每个参数添加参数定义,然后调用一个Execute的函数。不怎么想写这个定义,感觉没什么意思,不写又觉得不好,算了,全当练习打字了。还是举例子子吧!~~
当然我们要有个存储过程:
CREATE PROCEDURE [update_user_table_1]
(@id_1 [int],
@id_2 [int],
@name_3 [varchar](50),
@age_4 [int],
@sex_5 [char](10),
@profession_6 [varchar](50))
AS UPDATE [first_dotnet].[dbo].[user_table]
SET [id] = @id_2,
[name] = @name_3,
[age] = @age_4,
[sex] = @sex_5,
[profession]= @profession_6
WHERE
( [id] = @id_1)
GO
然后我们就要用这个存储过程
string connStr = "server=127.0.0.1;database=first_dotnet;User ID=sa;password=;Persist Security Info=true;";
SqlConnection con = new SqlConnection(connStr);
string procName = "update_user_table_1";
try
{
SqlCommand myCommand = new SqlCommand(procName, con);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@id_1", 5);
myCommand.Parameters.Add("@id_2", 21);
myCommand.Parameters.Add("@name_3", "小七");
myCommand.Parameters.Add(" @age_4,", 30);
myCommand.Parameters.Add(" @sex_5", "男");
myCommand.Parameters.Add("@profession_6 ", "会计");
if (con.State != ConnectionState.Open)
{
con.Open();
}
int updataCount = myCommand.ExecuteNonQuery();
con.Close();
Console.WriteLine("已经插入了" + updataCount + "条记录");
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
当然我们要有个存储过程:
CREATE PROCEDURE [update_user_table_1]
(@id_1 [int],
@id_2 [int],
@name_3 [varchar](50),
@age_4 [int],
@sex_5 [char](10),
@profession_6 [varchar](50))
AS UPDATE [first_dotnet].[dbo].[user_table]
SET [id] = @id_2,
[name] = @name_3,
[age] = @age_4,
[sex] = @sex_5,
[profession]= @profession_6
WHERE
( [id] = @id_1)
GO
然后我们就要用这个存储过程
string connStr = "server=127.0.0.1;database=first_dotnet;User ID=sa;password=;Persist Security Info=true;";
SqlConnection con = new SqlConnection(connStr);
string procName = "update_user_table_1";
try
{
SqlCommand myCommand = new SqlCommand(procName, con);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@id_1", 5);
myCommand.Parameters.Add("@id_2", 21);
myCommand.Parameters.Add("@name_3", "小七");
myCommand.Parameters.Add(" @age_4,", 30);
myCommand.Parameters.Add(" @sex_5", "男");
myCommand.Parameters.Add("@profession_6 ", "会计");
if (con.State != ConnectionState.Open)
{
con.Open();
}
int updataCount = myCommand.ExecuteNonQuery();
con.Close();
Console.WriteLine("已经插入了" + updataCount + "条记录");
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
相关文章推荐
- ADO.NET调用存储过程
- ADO.NET调用存储过程,过程比较全
- 如何使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
- 数据库连接+SQL语句+存储过程+ADO.Net调用存储过程
- HOW TO:使用 ADO.NET 和 Visual C++ .NET 调用带参数的存储过程
- C#二十六 使用Ado.Net调用存储过程
- [.NET]ADO.NET调用存储过程
- ADO.NET调用存储过程
- Ado.Net调用带输入,输出参数的存储过程
- C#二十六 使用Ado.Net调用存储过程
- ADO.net 调用存储过程
- 三层架构——存储过程(在ADO.net中调用存储过程)例子
- ADO.NET 第二课 事务和存储过程的调用(二)
- ado.net调用存储过程的一个封装类(网上找的不怕只怕自己不去理解)
- ADO.NET(五) 调用存储过程(oracle为例)
- C#二十六 使用Ado.Net调用存储过程
- ADO.NET调用存储过程,过程比较全
- ado.net调用存储过程
- ADO.NET调用存储过程,过程比较全
- C# - ADO.Net 调用存储过程