SqlConnection con=new SqlConnection("user id=sa;password=;initial catalog=game;data source=server1")
SqlDataAdapter adp=new SqlDataAdapter("p_game_countryToflag_all",con);
adp.SelectCommand.CommandType=CommandType.StoredProcedure;
adp.SelectCommand.Parameters.Add(new SqlParameter("@user_id",SqlDbType.Char,12));
adp.SelectCommand.Parameters["@user_id"].Value=user_id;
DataSet ds=new DataSet();
adp.Fill(ds,"rand");
p_game_countryToflag_all为存储过程名
@user_id 为参数
SqlDataAdapter adp=new SqlDataAdapter("p_game_countryToflag_all",con);
adp.SelectCommand.CommandType=CommandType.StoredProcedure;
adp.SelectCommand.Parameters.Add(new SqlParameter("@user_id",SqlDbType.Char,12));
adp.SelectCommand.Parameters["@user_id"].Value=user_id;
DataSet ds=new DataSet();
adp.Fill(ds,"rand");
p_game_countryToflag_all为存储过程名
@user_id 为参数
解决方案 »
- 怎么在asp:repeater添加判断语句呢?
- 功能超全,节日超多的日历代码。
- 将DataReader转换为DataTable时, 如何解决列名重复
- 为什么得不到Dropdownlist 的值
- 关于修改web.config碰到的一点问题,请大侠们帮帮忙!!!!!!!!!
- 更改服务器的名字
- DataList不显示空格的问题
- 急救:一个很有趣的confirm问题。。。。。
- 为什么double型的3.15+3.15却等于6.299999999呢
- 页面代码被自动修改的问题.
- 能不能编码永久改变Datagrid的结构,像“属性生成器”一样!即不用每次刷新页面都重新编码!
- 求助!!!!使用ASP.NET &C#编写IE/WEB版应用程序应该从何入手呢?????
{
SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=northwind;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter("Ten Most Expensive Products", myConnection); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet();
myCommand.Fill(ds, "产品"); MyDataGrid.DataSource=ds.Tables["产品"].DefaultView;
MyDataGrid.DataBind();
}
[Visual Basic]
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _
"Initial Catalog=northwind")Dim salesCMD As SqlCommand = New SqlCommand("SalesByCategory", nwindConn)
salesCMD.CommandType = CommandType.StoredProcedureDim myParm As SqlParameter = salesCMD.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15)
myParm.Value = "Beverages"nwindConn.Open()Dim myReader As SqlDataReader = salesCMD.ExecuteReader()Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1))Do While myReader.Read()
Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1))
LoopmyReader.Close()
nwindConn.Close()
[C#]
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");SqlCommand salesCMD = new SqlCommand("SalesByCategory", nwindConn);
salesCMD.CommandType = CommandType.StoredProcedure;SqlParameter myParm = salesCMD.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15);
myParm.Value = "Beverages";nwindConn.Open();SqlDataReader myReader = salesCMD.ExecuteReader();Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1));while (myReader.Read())
{
Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));
}myReader.Close();
nwindConn.Close();
OleDb
[Visual Basic]
Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;" & _
"Initial Catalog=northwind")Dim salesCMD As OleDbCommand = New OleDbCommand("SalesByCategory", nwindConn)
salesCMD.CommandType = CommandType.StoredProcedureDim myParm As OleDbParameter = salesCMD.Parameters.Add("@CategoryName", OleDbType.VarChar, 15)
myParm.Value = "Beverages"nwindConn.Open()Dim myReader As OleDbDataReader = salesCMD.ExecuteReader()Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1))Do While myReader.Read()
Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1))
LoopmyReader.Close()
nwindConn.Close()
[C#]
OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;" +
"Initial Catalog=northwind");OleDbCommand salesCMD = new OleDbCommand("SalesByCategory", nwindConn);
salesCMD.CommandType = CommandType.StoredProcedure;OleDbParameter myParm = salesCMD.Parameters.Add("@CategoryName", OleDbType.VarChar, 15);
myParm.Value = "Beverages";nwindConn.Open();OleDbDataReader myReader = salesCMD.ExecuteReader();Console.WriteLine("\t{0}, {1}", myReader.GetName(0), myReader.GetName(1));while (myReader.Read())
{
Console.WriteLine("\t{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));
}myReader.Close();
nwindConn.Close();
Parameter 对象可以使用 Parameter 构造函数来创建,或通过调用 Command 的 Parameters 集合的 Add 方法来创建。Parameters.Add 会将构造函数参数或现有 Parameter 对象用作输入。在将 Parameter 的 Value 设置为空引用时,请使用 DBNull.Value。对于 Input 参数之外的参数,必须设置 ParameterDirection 属性来指定参数类型是 InputOutput、Output 还是 ReturnValue。以下示例显示创建 Input、Output 和 ReturnValue 参数之间的差异。SqlClient
[Visual Basic]
Dim sampleCMD As SqlCommand = New SqlCommand("SampleProc", nwindConn)
sampleCMD.CommandType = CommandType.StoredProcedureDim sampParm As SqlParameter = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int)
sampParm.Direction = ParameterDirection.ReturnValuesampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12)
sampParm.Value = "Sample Value"sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28)
sampParm.Direction = ParameterDirection.OutputnwindConn.Open()Dim sampReader As SqlDataReader = sampleCMD.ExecuteReader()Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1))Do While sampReader.Read()
Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1))
LoopsampReader.Close()
nwindConn.Close()Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters("@OutputParm").Value)
Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters("RETURN_VALUE").Value)
SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn);
sampleCMD.CommandType = CommandType.StoredProcedure;SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
sampParm.Direction = ParameterDirection.ReturnValue;sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12);
sampParm.Value = "Sample Value";sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28);
sampParm.Direction = ParameterDirection.Output;nwindConn.Open();SqlDataReader sampReader = sampleCMD.ExecuteReader();Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1));while (sampReader.Read())
{
Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1));
}sampReader.Close();
nwindConn.Close();Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value);
Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
OleDb
[Visual Basic]
Dim sampleCMD As OleDbCommand = New OleDbCommand("SampleProc", nwindConn)
sampleCMD.CommandType = CommandType.StoredProcedureDim sampParm As OleDbParameter = sampleCMD.Parameters.Add("RETURN_VALUE", OleDbType.Integer)
sampParm.Direction = ParameterDirection.ReturnValuesampParm = sampleCMD.Parameters.Add("@InputParm", OleDbType.VarChar, 12)
sampParm.Value = "Sample Value"sampParm = sampleCMD.Parameters.Add("@OutputParm", OleDbType.VarChar, 28)
sampParm.Direction = ParameterDirection.OutputnwindConn.Open()Dim sampReader As OleDbDataReader = sampleCMD.ExecuteReader()Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1))Do While sampReader.Read()
Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1))
LoopsampReader.Close()
nwindConn.Close()Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters("@OutputParm").Value)
Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters("RETURN_VALUE").Value)
[C#]
OleDbCommand sampleCMD = new OleDbCommand("SampleProc", nwindConn);
sampleCMD.CommandType = CommandType.StoredProcedure;OleDbParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", OleDbType.Integer);
sampParm.Direction = ParameterDirection.ReturnValue;sampParm = sampleCMD.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
sampParm.Value = "Sample Value";sampParm = sampleCMD.Parameters.Add("@OutputParm", OleDbType.VarChar, 28);
sampParm.Direction = ParameterDirection.Output;nwindConn.Open();OleDbDataReader sampReader = sampleCMD.ExecuteReader();Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1));while (sampReader.Read())
{
Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1));
}sampReader.Close();
nwindConn.Close();Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value);
Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
将参数用于 SqlCommand
在将参数用于 SqlCommand 时,添加到 SqlParameterCollection 的参数的名称必须匹配存储过程中的参数标记的名称。SQL Server .NET 数据提供程序将存储过程中的参数当作命名参数并搜索匹配的参数标记。SQL Server .NET 数据提供程序不支持在向 SQL 语句或存储过程传递参数时使用问号 (?) 占位符。在这种情况下,必须使用命名参数,如以下示例所示。SELECT * FROM Customers WHERE CustomerID = @CustomerID
将参数用于 OleDbCommand
在将参数用于 OleDbCommand 时,添加到 OleDbParameterCollection 的参数的名称必须匹配存储过程中的参数标记的名称。OLE DB .NET 数据提供程序参数将存储过程中的参数当作命名参数并搜索匹配的参数标记。OLE DB .NET 数据提供程序不支持在向 SQL 语句或存储过程传递参数时使用命名参数。在此情况下,必须使用问号 (?) 占位符,如以下示例所示。SELECT * FROM Customers WHERE CustomerID = ?
因此,向 Parameters 集合添加 Parameter 对象的顺序必须直接对应于该参数的问号占位符的位置。导出参数信息
参数也可以使用 CommandBuilder 类从存储过程导出。SqlCommandBuilder 和 OleDbCommandBuilder 类都提供了静态方法 DeriveParameters,该静态方法将自动使用存储过程中的参数信息填充 Command 对象的 Parameters 集合。请注意,DeriveParameters 将改写 Command 的任何现有参数信息。导出参数信息时需要经历一个到数据源的附加行程,以获取参数信息。如果参数信息在设计时是已知的,则可以通过显式设置参数来提高应用程序的性能。以下代码示例显示如何使用 CommandBuilder.DeriveParameters 来填充 Command 对象的 Parameters 集合。[Visual Basic]
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;")
Dim salesCMD As SqlCommand = New SqlCommand("Sales By Year", nwindConn)
salesCMD.CommandType = CommandType.StoredProcedurenwindConn.Open()
SqlCommandBuilder.DeriveParameters(salesCMD)
nwindConn.Close()
[C#]
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
SqlCommand salesCMD = new SqlCommand("Sales By Year", nwindConn);
salesCMD.CommandType = CommandType.StoredProcedure;nwindConn.Open();
SqlCommandBuilder.DeriveParameters(salesCMD);
nwindConn.Close();