欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 中用 Sqlparameter 的兩種用法

 更新時(shí)間:2018年09月03日 15:09:44   作者:todo_something  
這篇文章主要介紹了C# 中用 Sqlparameter 的幾種用法,文中給大家列舉了兩種用法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

新建一個(gè)表:

create table abc
(
id int IDENTITY(1,1) NOT NULL,
name nvarchar(100) ,
sex nvarchar(10)
)
insert into abc values(‘a(chǎn)sf','男')
insert into abc values(‘a(chǎn)i','女')

創(chuàng)建表格完成。

新建一個(gè)存儲(chǔ)過程:

create procedure selbyid
(
@id int,
@thename nvarchar(100) output
)
as
select @thename= name from abc where id=@id

在執(zhí)行的過程中可以用sqlparameter 的幾種格式來調(diào)用存儲(chǔ)過程:

第一種是:

public string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;//存儲(chǔ)鏈接字符串,方便資源復(fù)用。
public SqlConnection getcon( )
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connString;
return conn;
}
private void btnsqlparauseing_Click(object sender, EventArgs e)
{
SqlConnection con = getcon();
con.Open();
string sqlstr = "insert into abc values(@name,@sex)"; //免除sql注入攻擊
SqlCommand cmd = new SqlCommand( );
cmd.Connection = con;
cmd.CommandText = sqlstr;
SqlParameter para = new SqlParameter(); //聲明參數(shù)
para= new SqlParameter("@name", SqlDbType.NVarChar,100);//生成一個(gè)名字為@Id的參數(shù),必須以@開頭表示是添加的參數(shù),并設(shè)置其類型長(zhǎng)度,類型長(zhǎng)度與數(shù)據(jù)庫(kù)中對(duì)應(yīng)字段相同,但是不能超出數(shù)據(jù)庫(kù)字段大小的范圍,否則報(bào)錯(cuò)。
para.Value = txtname.Text.ToString().Trim(); //這個(gè)是輸入?yún)?shù),所以可以賦值。
cmd.Parameters.Add(para);            //參數(shù)增加到cmd中。
para = new SqlParameter("@sex", SqlDbType.NVarChar, 10);
para.Value = txtsex.Text.ToString().Trim();
cmd.Parameters.Add(para);
int i =cmd.ExecuteNonQuery(); //執(zhí)行sql語句,并且返回影響的行數(shù)。
MessageBox.Show(i.ToString() + "命令完成行受影響插入成功", "提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
con.Close();
}

2.第二種是調(diào)用sqlparameter幾種方式來調(diào)用存儲(chǔ)過程:

1.

private void btnshuchu_Click(object sender, EventArgs e)
{
SqlConnection con = getcon();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "selbyid"; //存儲(chǔ)過程的名稱
cmd.CommandType = CommandType.StoredProcedure; //說明是存儲(chǔ)過程
SqlParameter para = new SqlParameter();     //聲明sqlparameter參數(shù)
para = new SqlParameter("@id", SqlDbType.Int); //這個(gè)參數(shù)是輸入?yún)?shù)
para.Value = int.Parse(txtid.Text.ToString().Trim()); //因?yàn)槭禽斎雲(yún)?shù)所以可以賦值
cmd.Parameters.Add(para); //加入cmd中
para=new SqlParameter("@thename",SqlDbType.NVarChar,100);//參數(shù)的大小可以小于數(shù)據(jù)庫(kù)的參數(shù)規(guī)定值,但不能夠大于數(shù)據(jù)庫(kù)的參數(shù)大小。
cmd.Parameters.Add(para); //和下面一句不可掉亂,先增加再指明它是輸出參數(shù)來的。
cmd.Parameters["@thename"].Direction = ParameterDirection.Output; //增加后,用output說明是輸出參數(shù)。
int i=cmd.ExecuteNonQuery();
string name = cmd.Parameters["@thename"].Value.ToString(); //經(jīng)過執(zhí)行,存儲(chǔ)過程返回了輸出參數(shù)。
MessageBox.Show("命令完成 " + name + "是所查記錄", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
}

套路就是: 輸出參數(shù)先聲明,再賦值,再加入cmd的參數(shù)中,最后用cmd.ExecuteNonQuery()執(zhí)行。

2.用AddWithValue:

private void btnothers_Click(object sender, EventArgs e)
{
SqlConnection con = getcon();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "selbyid";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter para = new SqlParameter();
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(txtid.Text.Trim()));//輸入?yún)?shù)可以用addWithValue來格式化參數(shù),但輸出參數(shù)只能用Add
cmd.Parameters.Add("@thename", SqlDbType.NVarChar,100).Direction = ParameterDirection.Output; //和下面一句不可順序掉亂,否則會(huì)報(bào)錯(cuò),先加入cmd中再指明它是輸出參數(shù)來的。
con.Open();
int i = cmd.ExecuteNonQuery();
string name = cmd.Parameters["@thename"].Value.ToString(); //輸出參數(shù)返回一個(gè)數(shù)值。
MessageBox.Show("命令完成 " + name + "是所查記錄", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
}

3.用參數(shù)數(shù)組實(shí)現(xiàn)調(diào)用輸入和輸出參數(shù)的存儲(chǔ)過程:

private void btnshuzu_Click(object sender, EventArgs e)
{
SqlConnection con = getcon();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "selbyid";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] para = { new SqlParameter("@id", SqlDbType.Int)};
para[0].Value = Convert.ToInt32(txtid.Text.ToString().Trim());
cmd.Parameters.AddRange(para); //輸入?yún)?shù)和輸出參數(shù)分別加入到cmd.Parameter中。
cmd.Parameters.Add("@thename",SqlDbType.NVarChar,100).Direction = ParameterDirection.Output; //和下面一句不可掉亂,先增加再指明它是輸出參數(shù)來的。   
con.Open();
int i = cmd.ExecuteNonQuery();
string name = cmd.Parameters["@thename"].Value.ToString();
MessageBox.Show("命令完成 " + name + "是所查記錄", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
}

總結(jié)

以上所述是小編給大家介紹的C# 中用 Sqlparameter 的兩種用法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論