Asp.net 如何在使用c#.net的SQL Server 2008中使用1个insert存储过程插入5行
我必须使用Asp.net在一个表中保存5个文件路径,并使用一次存储过程调用,后端是SQL Server 2008 如果使用表值参数,则非常欢迎 请尽快回复我Asp.net 如何在使用c#.net的SQL Server 2008中使用1个insert存储过程插入5行,asp.net,sql-server-2008,c#-4.0,Asp.net,Sql Server 2008,C# 4.0,我必须使用Asp.net在一个表中保存5个文件路径,并使用一次存储过程调用,后端是SQL Server 2008 如果使用表值参数,则非常欢迎 请尽快回复我 谢谢。假设您有这样一张桌子: CREATE TABLE dbo.FileTest ( ID INT NOT NULL IDENTITY(1,1), FileName VARCHAR(255), DateInserted DATETIME2(3) ) // define some strings we n
谢谢。假设您有这样一张桌子:
CREATE TABLE dbo.FileTest
(
ID INT NOT NULL IDENTITY(1,1),
FileName VARCHAR(255),
DateInserted DATETIME2(3)
)
// define some strings we need - connection string, name of stored procedure, name of the table type
string connectionString = "server=.;Database=test;Integrated Security=SSPI;";
string storedProcedureName = "dbo.InsertFileNames";
string tvpValueTypeName = "FileNameType";
// set up data to insert - create a DataTable that has the same structure as your TVP type in SQL Server
DataTable filesNamesToAdd = new DataTable();
filesNamesToAdd.Columns.Add(new DataColumn("FileName", typeof (string)));
// insert some rows into that DataTable
DataRow row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\autoexec.cmd";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\Explorer.exe";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\starter.xml";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\WindowsUpdate.log";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\mip.bin";
filesNamesToAdd.Rows.Add(row);
// set up SqlConnection and SqlCommand in using blocks
using(SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand insertCommand = new SqlCommand(storedProcedureName, conn))
{
// define the SqlCommand to be a stored procedure we're calling
insertCommand.CommandType=CommandType.StoredProcedure;
// define the table-valued parameter; it's of type SqlDbType.Structured, and you need to tell it the TVP type in SQL Server
SqlParameter tvpParam = insertCommand.Parameters.Add("@TVP", SqlDbType.Structured);
tvpParam.TypeName = tvpValueTypeName;
tvpParam.Value = filesNamesToAdd;
// open connection, call the stored procedure to insert data, close connection
conn.Open();
insertCommand.ExecuteNonQuery();
conn.Close();
}
然后您需要创建一个表类型,它可以用作表值参数:
CREATE TYPE FileNameType AS TABLE (FileName VARCHAR(255));
接下来,您需要定义一个以该表类型为参数的存储过程,并将来自TVP的数据插入表中:
CREATE PROCEDURE dbo.InsertFileNames
@TVP FileNameType READONLY
AS
SET NOCOUNT ON
INSERT INTO FileTest(FileName, DateInserted)
SELECT FileName, Sysdatetime()
FROM @TVP;
现在,在C#代码中,需要有如下内容:
CREATE TABLE dbo.FileTest
(
ID INT NOT NULL IDENTITY(1,1),
FileName VARCHAR(255),
DateInserted DATETIME2(3)
)
// define some strings we need - connection string, name of stored procedure, name of the table type
string connectionString = "server=.;Database=test;Integrated Security=SSPI;";
string storedProcedureName = "dbo.InsertFileNames";
string tvpValueTypeName = "FileNameType";
// set up data to insert - create a DataTable that has the same structure as your TVP type in SQL Server
DataTable filesNamesToAdd = new DataTable();
filesNamesToAdd.Columns.Add(new DataColumn("FileName", typeof (string)));
// insert some rows into that DataTable
DataRow row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\autoexec.cmd";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\Explorer.exe";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\starter.xml";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\WindowsUpdate.log";
filesNamesToAdd.Rows.Add(row);
row = filesNamesToAdd.NewRow();
row["FileName"] = "C:\\Windows\\mip.bin";
filesNamesToAdd.Rows.Add(row);
// set up SqlConnection and SqlCommand in using blocks
using(SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand insertCommand = new SqlCommand(storedProcedureName, conn))
{
// define the SqlCommand to be a stored procedure we're calling
insertCommand.CommandType=CommandType.StoredProcedure;
// define the table-valued parameter; it's of type SqlDbType.Structured, and you need to tell it the TVP type in SQL Server
SqlParameter tvpParam = insertCommand.Parameters.Add("@TVP", SqlDbType.Structured);
tvpParam.TypeName = tvpValueTypeName;
tvpParam.Value = filesNamesToAdd;
// open connection, call the stored procedure to insert data, close connection
conn.Open();
insertCommand.ExecuteNonQuery();
conn.Close();
}
完成了