在检索主键时遇到问题 [英] having problem in retrieving primary key

查看:107
本文介绍了在检索主键时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索父表的主键然后将此密钥发送到子表,因为它们具有一对一的关系

我已经编写了代码


string id =" SELECT MAX(c_id)FROM call_history" ;;

SqlCommand com1 = new SqlCommand(id,conn);

SqlDataReader dr = com1。 ExecuteReader();

if(dr.Read())

{

int i = Convert.ToInt32(dr.GetValue(1) );


}


strQuery1 =" INSERT INTO call_log(c_id,c_add)VALUES(" + i +",' '" + textBox2.Text +"'')" ;;


SqlCommand commm = new SqlCommand(strQuery1,conn);


commm.ExecuteNonQuery();


但它在插入atement时出错了

"使用未分配的局部变量''我''"

i want to retrieve primary key of parent table and then send this key to child table,as they have one-one relationship
i''ve written code

string id = "SELECT MAX(c_id) FROM call_history";
SqlCommand com1 = new SqlCommand(id, conn);
SqlDataReader dr = com1.ExecuteReader();
if (dr.Read())
{
int i = Convert.ToInt32(dr.GetValue(1));

}

strQuery1 = "INSERT INTO call_log(c_id,c_add)VALUES("+ i +",''"+textBox2.Text +"'')";

SqlCommand commm = new SqlCommand(strQuery1, conn);

commm.ExecuteNonQuery();


but it is goiving an error on insert atement that
"Use of unassigned local variable ''i'' "

推荐答案


i想要检索父表的主键,然后将此密钥发送到子表,因为它们具有一对一的关系

我已经编写了代码


string id =" SELECT MAX(c_id)FROM call_history" ;;

SqlCommand com1 = new SqlCommand(id ,conn);

SqlDataReader dr = com1.ExecuteReader();

if(dr.Read())

{

int i = Convert.ToInt32(dr.GetValue(1));


}


strQuery1 =" INSERT INTO call_log(c_id,c_add)VALUES(" + i +",''" + textBox2.Text +"'')" ;;


SqlCommand commm = new SqlCommand (strQuery1,conn);


commm.ExecuteNonQuery();


但它在插入atement时出错了

"使用未分配的局部变量''i''
i want to retrieve primary key of parent table and then send this key to child table,as they have one-one relationship
i''ve written code

string id = "SELECT MAX(c_id) FROM call_history";
SqlCommand com1 = new SqlCommand(id, conn);
SqlDataReader dr = com1.ExecuteReader();
if (dr.Read())
{
int i = Convert.ToInt32(dr.GetValue(1));

}

strQuery1 = "INSERT INTO call_log(c_id,c_add)VALUES("+ i +",''"+textBox2.Text +"'')";

SqlCommand commm = new SqlCommand(strQuery1, conn);

commm.ExecuteNonQuery();


but it is goiving an error on insert atement that
"Use of unassigned local variable ''i'' "



查看你的代码。你在if块中定义了i,但是你试图在if块之外访问它。

Look at your code. You defined i inside the if block but you are trying to access it outside that if block.



查看你的代码。你在if块中定义了i,但是你试图在if块之外访问它。
Look at your code. You defined i inside the if block but you are trying to access it outside that if block.



这次我定义了我的全局,但仍然有同样的问题......插入语句有什么问题吗?

有我写了sql命令rite ?? /


this time i defined i globaly but still having the same problem...is there any problem in the insert statement??
have i written the sql command rite??/



i想要检索父表的主键,然后将此密钥发送到子表,如他们有一对一的关系

我写了代码


string id =" SELECT MAX(c_id)FROM call_history" ;;

SqlCommand com1 = new SqlCommand(id,conn);

SqlDataReader dr = com1.ExecuteReader();

if(dr.Read())

{

int i = Convert.ToInt32(dr.GetValue(1));


}

strQuery1 =" INSERT INTO call_log(c_id,c_add)VALUES(" + i +",''" + textBox2.Text +"'')" ;;


SqlCommand commm = new SqlCommand(strQuery1,conn);


commm.ExecuteNonQuery();


但是它是插入atement的错误

使用未分配的局部变量''我''
i want to retrieve primary key of parent table and then send this key to child table,as they have one-one relationship
i''ve written code

string id = "SELECT MAX(c_id) FROM call_history";
SqlCommand com1 = new SqlCommand(id, conn);
SqlDataReader dr = com1.ExecuteReader();
if (dr.Read())
{
int i = Convert.ToInt32(dr.GetValue(1));

}

strQuery1 = "INSERT INTO call_log(c_id,c_add)VALUES("+ i +",''"+textBox2.Text +"'')";

SqlCommand commm = new SqlCommand(strQuery1, conn);

commm.ExecuteNonQuery();


but it is goiving an error on insert atement that
"Use of unassigned local variable ''i'' "



你在全球范围内宣布.......赞

you are declaring i globally.......like

展开 | 选择 | 换行 | 行号


这篇关于在检索主键时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆