示例
存储过程是:
CREATE Procedure CustomerAdd
(
@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50),
)
ASINSERT INTO Customers
(
FullName,
EMailAddress,
Password
)VALUES
(
@FullName,
@Email,
@Password
)RETURN @@IdentityGO
C#代码为:
using System;
using System.Data;
using System.Data.SqlClient;public class AddCustomer
{
public static void Main()
{
string connectionString = "Data Source=localhost;" +
"Initial Catalog=store;Integrated Security=SSPI";
string procedure = "CustomerAdd"; // Create ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(procedure, con); // Configure the command.
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param; // Add the parameter representing the return value.
param = cmd.Parameters.Add("@CustomerID", SqlDbType.Int);
param.Direction = ParameterDirection.ReturnValue; // Add the input parameters.
param = cmd.Parameters.Add("@FullName", SqlDbType.NVarChar, 50);
param.Value = "John Smith"; param = cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50);
param.Value = "[email protected]"; param = cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50);
param.Value = "opensesame"; // Execute the command.
con.Open();
cmd.ExecuteNonQuery();
con.Close(); param = cmd.Parameters["@CustomerID"];
Console.WriteLine("New customer has ID of " + param.Value); }
}
存储过程是:
CREATE Procedure CustomerAdd
(
@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50),
)
ASINSERT INTO Customers
(
FullName,
EMailAddress,
Password
)VALUES
(
@FullName,
@Email,
@Password
)RETURN @@IdentityGO
C#代码为:
using System;
using System.Data;
using System.Data.SqlClient;public class AddCustomer
{
public static void Main()
{
string connectionString = "Data Source=localhost;" +
"Initial Catalog=store;Integrated Security=SSPI";
string procedure = "CustomerAdd"; // Create ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(procedure, con); // Configure the command.
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param; // Add the parameter representing the return value.
param = cmd.Parameters.Add("@CustomerID", SqlDbType.Int);
param.Direction = ParameterDirection.ReturnValue; // Add the input parameters.
param = cmd.Parameters.Add("@FullName", SqlDbType.NVarChar, 50);
param.Value = "John Smith"; param = cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50);
param.Value = "[email protected]"; param = cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50);
param.Value = "opensesame"; // Execute the command.
con.Open();
cmd.ExecuteNonQuery();
con.Close(); param = cmd.Parameters["@CustomerID"];
Console.WriteLine("New customer has ID of " + param.Value); }
}
解决方案 »
- win7系统 为什么装不上Visual Studio 2010旗舰版 非常感谢!
- c#winform实现动态曲线图
- c# winform 网络软件是怎么连接数据库的
- smtp的问题
- c#中有几种集合类型?
- 帮我看看这个函数有问题吗?为什么返回的是 -1
- *********求解treeview的问题**********
- SQL Server 2008 数据库再一次打不开了
- winform的DataGrid如何添加一个RadioButton列,实现单选?
- 我做了一个组件,但是出错的话,他会把我整个代码都抛出来,我不想他抛代码出来。
- 请教关于数组拷贝的问题?
- 北京的c#程序员工资都是多少啊????????
结帖。