问题
public void updateSkills(DataTable candidateSkillSets)
{
string sqlCommand = "Sp_Candidate";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(dbCommand, "candidateSkillSets",DbType.Object, candidateSkillSets);
db.ExecuteNonQuery(dbCommand);
}
I have a method like the above, here i am passing the datatable to the stored procedure by adding the parameter."DbType.Object" is not taking the datatable type. I know in ADO we can use "SqlDbType.Structured", but for enterprise libray it is not working. What i have to use instead?
I am getting the following error while executing the command
"The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 1 ("@candidateSkillSets"): Data type 0x62 (sql_variant) has an invalid type for type-specific metadata."
回答1:
I have to modify @eduardo's code to make me work in my case with Enterprise Library:
public int Insert(int id, DTO mnemonics)
{
DatabaseProviderFactory factory = new DatabaseProviderFactory();
Database Db = factory.CreateDefault();
using (var dbCommand = Db.GetStoredProcCommand("spINSERT"))
{
using (var table = new DataTable())
{
table.Columns.Add("Key", typeof(string));
table.Columns.Add("Value", typeof(string));
foreach (KeyValuePair<string, string> item in mnemonics.data)
{
var row = table.NewRow();
row["Key"] = item.Key;
row["Value"] = item.Value;
table.Rows.Add(row);
}
Db.AddInParameter(dbCommand, "id", DbType.int, id);
SqlParameter p = new SqlParameter("MNEMONICS", table);
p.SqlDbType = SqlDbType.Structured;
dbCommand.Parameters.Add(p);
Db.ExecuteNonQuery(dbCommand);
}
}
}
My type in SQL Server:
CREATE TYPE [dbo].[StringDict] AS TABLE(
[Key] [varchar](max) NULL,
[Value] [varchar](max) NULL
)
I hope help someone
回答2:
"DbType.Structured" Table valued parameters are not supported in the enterprise library . If you want to use a table as a parameter to a stored proc or query, you'll have to use the underlying store connection and the support that is provided there.
回答3:
DbType.Structured Table valued parameters are not supported in the enterprise library, but it is posible to used this way:
db = DatabaseFactory.CreateDatabase();
using (DbCommand cmd = db.GetStoredProcCommand("Sp_Candidate"))
{
db.AddInParameter(cmd, "@candidateid", DbType.Int32, txtCandidateID.text);
cmd.Parameters.Add(new SqlParameter("candidateSkillSets", candidateSkillSets) { SqlDbType = SqlDbType.Structured });
db.ExecuteNonQuery(cmd);
}
How to pass an array into a SQL Server stored procedure
来源:https://stackoverflow.com/questions/30662943/pass-data-table-to-sql-server-from-asp-net-using-enterprise-library