本文整理匯總了C#中System.Data.SqlClient.SqlCommand.Parameters屬性的典型用法代碼示例。如果您正苦於以下問題:C# SqlCommand.Parameters屬性的具體用法?C# SqlCommand.Parameters怎麽用?C# SqlCommand.Parameters使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Data.SqlClient.SqlCommand
的用法示例。
在下文中一共展示了SqlCommand.Parameters屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: UpdateDemographics
private static void UpdateDemographics(Int32 customerID,
string demoXml, string connectionString)
{
// Update the demographics for a store, which is stored
// in an xml column.
string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
+ "WHERE CustomerID = @ID;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters["@ID"].Value = customerID;
// Use AddWithValue to assign Demographics.
// SQL Server will implicitly convert strings into XML.
command.Parameters.AddWithValue("@demographics", demoXml);
try
{
connection.Open();
Int32 rowsAffected = command.ExecuteNonQuery();
Console.WriteLine("RowsAffected: {0}", rowsAffected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
示例2: Main
//引入命名空間
using System;
using System.Data;
using System.Data.SqlClient;
class CommandExampleParameters
{
static void Main()
{
SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
SqlCommand nonqueryCommand = thisConnection.CreateCommand();
try {
thisConnection.Open();
nonqueryCommand.CommandText = "CREATE TABLE MyTable (MyName VARCHAR (30), MyNumber integer)";
Console.WriteLine(nonqueryCommand.CommandText);
Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());
nonqueryCommand.CommandText = "INSERT INTO MyTable VALUES (@MyName, @MyNumber)";
nonqueryCommand.Parameters.Add("@MyName", SqlDbType.VarChar, 30);
nonqueryCommand.Parameters.Add("@MyNumber", SqlDbType.Int);
nonqueryCommand.Prepare();
string[] names = { "A", "B", "C", "D" } ;
int i;
for (i=1; i<=4; i++) {
nonqueryCommand.Parameters["@MyName"].Value = names[i-1];
nonqueryCommand.Parameters["@MyNumber"].Value = i;
Console.WriteLine(nonqueryCommand.CommandText);
Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());
}
} catch (SqlException ex) {
Console.WriteLine(ex.ToString());
} finally {
thisConnection.Close();
Console.WriteLine("Connection Closed.");
}
}
}