在检索主键时遇到问题

在检索主键时遇到问题

问题描述:

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

我已经编写了代码


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

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 +"'')&quot ;;


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&quot ;;

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 +"'')&quot ;;


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&quot ;;

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 +"'')&quot ;;


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

展开 | / span> 选择 | 换行 | 行号