![](/img/trans.png)
[英]How to create a SQL CLR stored procedure to get data from a web service and insert results into a SQL Server table
[英]How to insert data into a sql data table via stored procedure
我想将来自asp.net中的数据集的数据插入到SQL Server表中。
但是我无法将值从数据集传递到我的存储过程中,请帮助
这是我的代码
private static SqlCommand WriteDatabase(SqlConnection conn)
{
SqlCommand cmd = new SqlCommand(SP_insertData);
cmd.Parameters.Clear();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameterCollection pc = cmd.Parameters;
pc.Add(CreateParameter("abID", System.Data.SqlDbType.Int));
pc.Add(CreateParameter("fHitType", System.Data.SqlDbType.Int));
pc.Add(CreateParameter("DateOfHit", System.Data.SqlDbType.DateTime));
pc.Add(CreateParameter("TimeOfHit", System.Data.SqlDbType.Int));
pc.Add(CreateParameter("fEmpid", System.Data.SqlDbType.Int));
cmd.ExecuteNonQuery();
return cmd;
}
private static SqlParameter CreateParameter(string p, SqlDbType sqlDbType)
{
SqlParameter parameter = new SqlParameter("@" + p, sqlDbType);
parameter.SourceColumn = p;
return parameter;
}
我无法将值从数据集传递到存储过程
您是否尝试过以下代码:
SqlConnection oConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString);
public Boolean InserData(string abID, string fHitType,string DateOfHit, string TimeOfHit,string fEmpid)
{
// SqlConnection oConnection = new SqlConnection("Connection_String");
SqlCommand oCommand = new SqlCommand();
oCommand.Connection = oConnection;
oCommand.CommandText = "SP_insertData";
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.Parameters.AddWithValue("@abID", abID);
oCommand.Parameters.AddWithValue("@fHitType", fHitType);
oCommand.Parameters.AddWithValue("@DateOfHit", DateOfHit);
oCommand.Parameters.AddWithValue("@TimeOfHit", TimeOfHit);
oCommand.Parameters.AddWithValue("@fEmpid", fEmpid);
oConnection.Open();
Boolean Result = Convert.ToBoolean(oCommand.ExecuteNonQuery());
oConnection.Close();
return Result;
}
它将返回true或false返回true表示您的数据已成功保存,返回false表示未保存...
请尝试一下
SqlConnection oConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString);
public int InserData(string abID, string fHitType, string DateOfHit, string TimeOfHit, string fEmpid)
{
SqlCommand oCommand = new SqlCommand("SP_insertData", oConnection);
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.Parameters.AddWithValue("@abID", abID);
oCommand.Parameters.AddWithValue("@fHitType", fHitType);
oCommand.Parameters.AddWithValue("@DateOfHit", DateOfHit);
oCommand.Parameters.AddWithValue("@TimeOfHit", TimeOfHit);
oCommand.Parameters.AddWithValue("@fEmpid", fEmpid);
oConnection.Open();
int Result = oCommand.ExecuteNonQuery();
oConnection.Close();
return Result;
}
如果返回类型为1,则说明否则未成功插入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.