C#中“parameter”类的用法

如题
2024-11-27 15:51:13
推荐回答(1个)
回答(1):

MSDN 里有大把例子,如
// Assumes that connection represents a SqlConnection object.

SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT CategoryID, CategoryName FROM dbo.Categories", connection);

adapter.InsertCommand = new SqlCommand("InsertCategory", connection);
adapter.InsertCommand.CommandType = CommandType.StoredProcedure;

SqlParameter parameter = adapter.InsertCommand.Parameters.Add(
"@RowCount", SqlDbType.Int);
parameter.Direction = ParameterDirection.ReturnValue;

adapter.InsertCommand.Parameters.Add(
"@CategoryName", SqlDbType.NChar, 15, "CategoryName");

parameter = adapter.InsertCommand.Parameters.Add(
"@Identity", SqlDbType.Int, 0, "CategoryID");
parameter.Direction = ParameterDirection.Output;

DataSet categoriesDS = new DataSet();
adapter.Fill(categoriesDS, "Categories");

DataRow newRow = categoriesDS.Tables["Categories"].NewRow();
newRow["CategoryName"] = "New Category";
categoriesDS.Tables["Categories"].Rows.Add(newRow);

adapter.Update(categoriesDS, "Categories");

Int32 rowCount = (Int32)adapter.InsertCommand.Parameters["@RowCount"].Value;