如何在存储过程中使用变量作为参数
问题描述:
hiii all,我已经写了这段代码。
hiii all, i have write this code.
GridViewRow row = GridView1.Rows[index];
var text = GridView1.Rows[index].Cells[0].Text;
string tb = ((TextBox)GridView1.Rows[index].FindControl("txt1")).Text;
String[] sentences = Regex.Split(text, @"(?<=[.!?])\s+(?=\p{Lt})");
System.Data.DataTable dt1 = new System.Data.DataTable();
dt1.Columns.Add("SentenceText1");
foreach (string value in sentences)
{
if(!string.IsNullOrWhiteSpace(value))
dt1.Rows.Add(value);
}
SqlConnection con = new SqlConnection(conn);
con.Open();
string sql = "";
for (int i = 0; i < dt1.Rows.Count; i++)
{
var abc = dt1.Rows[i]["SentenceText1"].ToString();
--------------------- call storeprocedure -------- ----------------------------
现在我想在商店程序中使用abc来插入这些数据表句子所以如何用输入参数abc写出存储过程。
--------------------- call storeprocedure ------------------------------------
now i want to use abc in store procedure to insert this data into table "sentence " so how can i write store procedure with input parameter abc.
答
试试:
Try:
CREATE PROC [dbo].Sample
@UserName varchar(100)
AS
BEGIN
SELECT Id FROM myTable WHERE UserName = @UserName
END
然后调用它:
Then call it:
int id = -1;
using (SqlCommand com = new SqlCommand("Sample", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@UserName", abc);
using (SqlDataReader read = com.ExecuteReader())
{
if (read.Read()) id = (int) read["Id"];
}
}