在数据库编程中出现列名无效的错误,到底是什么原因啊? 在SQL数据库中设计的列名有的是中文,在数据库编程的时候出现了'某某列名'无效的错误.请问这是什么回事? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void Button2_Click(object sender, System.EventArgs e) { int index=1; foreach(DataGridItem Mydg in DataGrid1.Items) { int thenum1=0,thenum2=0,thenum3=0,thenum4=0; bool fir=false,sec=false,thir=false,forth=false; string sqlstr="UPDATE vote SET "; string selstr="SELECT "; string sqlstr1="",sqlstr2="",sqlstr3="",sqlstr4=""; string selstr1="",selstr2="",selstr3="",selstr4=""; if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="好") { sqlstr1="'思想好' = @num1"; selstr1="'思想好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="比较好") { sqlstr1="'思想较好' = @num1"; selstr1="'思想较好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="一般") { sqlstr1="'思想一般' = @num1"; selstr1="'思想一般'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="比较差") { sqlstr1="'思想较差' = @num1"; selstr1="'思想较差'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="差") { sqlstr1="'思想差' = @num1"; selstr1="'思想差'"; } if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="好") { sqlstr2="'作风好' = @num2"; selstr2="'作风好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="比较好") { sqlstr2="'作风较好' = @num2"; selstr2="'作风较好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="一般") { sqlstr2="'作风一般' = @num2"; selstr2="'作风一般'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="比较差") { sqlstr2="'作风较差' = @num2"; selstr2="'作风较差'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="差") { sqlstr2="'作风差' = @num2"; selstr2="'作风差'"; } if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="好") { sqlstr3="'能力好' = @num3"; selstr3="'能力好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="比较好") { sqlstr3="'能力较好' = @num3"; selstr3="'能力较好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="一般") { sqlstr3="'能力一般' = @num3"; selstr3="'能力一般'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="比较差") { sqlstr3="'能力较差' = @num3"; selstr3="'能力较差'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="差") { sqlstr3="'能力差' = @num3"; selstr3="'能力差'"; } if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="好") { sqlstr4="'廉洁好' = @num4"; selstr4="'廉洁好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="比较好") { sqlstr4="'廉洁较好' = @num4"; selstr4="'廉洁较好'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="一般") { sqlstr4="'廉洁一般' = @num4"; selstr4="'廉洁一般'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="比较差") { sqlstr4="'廉洁较差' = @num4"; selstr4="'廉洁较差'"; } else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="差") { sqlstr4="'廉洁差' = @num4"; selstr4="'廉洁差'"; } if(sqlstr1!="") { sqlstr+=sqlstr1; fir=true; } if(sqlstr2!="") { if(fir) { sqlstr+=","; sqlstr+=sqlstr2; } else { sqlstr+=sqlstr2; } sec=true; } if(sqlstr3!="") { if(fir||sec) { sqlstr+=","; sqlstr+=sqlstr3; } else { sqlstr+=sqlstr3; } thir=true; } if(sqlstr4!="") { if(fir||sec||thir) { sqlstr+=","; sqlstr+=sqlstr4; } else { sqlstr+=sqlstr4; } forth=true; } if(fir) { selstr+=selstr1; } else { selstr+="'思想一般'"; } if(sec) { selstr+=","; selstr+=selstr2; } else { selstr+=",'作风一般'"; } if(thir) { selstr+=","; selstr+=selstr3; } else { selstr+=",'能力一般'"; } if(forth) { selstr+=","; selstr+=selstr4; } else { selstr+=",'廉洁一般'"; } selstr+=" WHERE '编号'=@indx1"; cmdsel.CommandText=selstr; cmdsel.Parameters.Add(new SqlParameter("@indx1",SqlDbType.Int,4)); cmdsel.Parameters["@indx1"].Value=index; sqlConnection1.Open(); SqlDataReader dr; dr=cmdsel.ExecuteReader(); sqlstr+=" WHERE '编号'=@indx2"; cmdupdate.CommandText=sqlstr; cmdupdate.Parameters.Add(new SqlParameter("@indx2",SqlDbType.Int,4)); cmdupdate.Parameters["@indx2"].Value=index; if(dr.Read()) { if(fir) { thenum1=dr.GetInt16(0); cmdupdate.Parameters.Add(new SqlParameter("@num1",SqlDbType.Int,16)); cmdupdate.Parameters["@num1"].Value=++thenum1; } if(sec) { thenum2=dr.GetInt16(1); cmdupdate.Parameters.Add(new SqlParameter("@num2",SqlDbType.Int,16)); cmdupdate.Parameters["@num2"].Value=++thenum2; } if(thir) { thenum3=dr.GetInt16(2); cmdupdate.Parameters.Add(new SqlParameter("@num3",SqlDbType.Int,16)); cmdupdate.Parameters["@num3"].Value=++thenum3; } if(forth) { thenum4=dr.GetInt16(3); cmdupdate.Parameters.Add(new SqlParameter("@num4",SqlDbType.Int,16)); cmdupdate.Parameters["@num4"].Value=++thenum4; } } dr.Close(); sqlConnection1.Close(); if(fir||sec||thir||forth) { sqlConnection1.Open(); cmdupdate.ExecuteNonQuery(); sqlConnection1.Close(); } index++; } } }} 我是想做一个投票系统.请问编程的时候中文列名是不是要加上单引号?因为没加单引号的时候会提示列名有误,加了就没有了.不过有新的错误:将 varchar 值 '编号' 转换为数据类型为 int 的列时发生语法错误.其中编号是一个列名,其数据类型为int型. 可能是因为,你的数据库中途修改过,而你的dataset2中没有这个字段。解决的方法是,在查询语句中加上这个字段。 是指dataset里的字段和数据库里的字段吗?还有我的程序是不是有什么问题啊? 现在的错误是:将 varchar 值 '编号' 转换为数据类型为 int 的列时发生语法错误。为什么会有:将 varchar 值 '编号' 转换为数据类型为 int这个情况啊?难道是编号这里加了单引号?但是如果没有加单引号,就会出现:'编号'列无效的错误.真是郁闷啊.为什么?高手们真的要来救救我了,我要疯了!!! 我想问一下,在SQL语句中的中文列名是不是要用单引号不括起来啊?能帮我解决这个问题的人怎么还没有出现啊? 列名无效,你单步调试,到sqlConnection1.Open(); cmdupdate.ExecuteNonQuery();看cmdupdate的commmodText是什么,然后取到查询分析器里面,看什么字段(列名没有) 请问有中文列名的SQL语句应该怎么写?比如有3个列名:姓名、单位、职务。给我分别写出它的SELECT、UPDATE、INSERT语句。回上面两位,我在DATASET里面已经选了所有的字段了,应该不不可能出现字段没有的情况吧。 是怎样进行SQL注入的??? ASP.NET checkboxlist 的一个问题? 问个sql语句的意思.糊涂了.... ASP 做数据库系统 问下下asp.net中session变量利用数据库查询的元素赋值的问题 【求救】验证控件无法找到脚本库?应该怎么办啊 急求救! radiobutton和gridview怎么一起使用? vs.net的IDE中HTML的按钮看不见了,如何上他出来? 各位有没有碰到过这种问题! 问一个很小的问题! 把数据库中的新闻内容放入ifram怎样实现?
{
int index=1;
foreach(DataGridItem Mydg in DataGrid1.Items)
{
int thenum1=0,thenum2=0,thenum3=0,thenum4=0;
bool fir=false,sec=false,thir=false,forth=false;
string sqlstr="UPDATE vote SET ";
string selstr="SELECT ";
string sqlstr1="",sqlstr2="",sqlstr3="",sqlstr4="";
string selstr1="",selstr2="",selstr3="",selstr4="";
if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="好")
{
sqlstr1="'思想好' = @num1";
selstr1="'思想好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="比较好")
{
sqlstr1="'思想较好' = @num1";
selstr1="'思想较好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="一般")
{
sqlstr1="'思想一般' = @num1";
selstr1="'思想一般'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="比较差")
{
sqlstr1="'思想较差' = @num1";
selstr1="'思想较差'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList1")).SelectedValue=="差")
{
sqlstr1="'思想差' = @num1";
selstr1="'思想差'";
} if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="好")
{
sqlstr2="'作风好' = @num2";
selstr2="'作风好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="比较好")
{
sqlstr2="'作风较好' = @num2";
selstr2="'作风较好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="一般")
{
sqlstr2="'作风一般' = @num2";
selstr2="'作风一般'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="比较差")
{
sqlstr2="'作风较差' = @num2";
selstr2="'作风较差'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList2")).SelectedValue=="差")
{
sqlstr2="'作风差' = @num2";
selstr2="'作风差'";
} if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="好")
{
sqlstr3="'能力好' = @num3";
selstr3="'能力好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="比较好")
{
sqlstr3="'能力较好' = @num3";
selstr3="'能力较好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="一般")
{
sqlstr3="'能力一般' = @num3";
selstr3="'能力一般'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="比较差")
{
sqlstr3="'能力较差' = @num3";
selstr3="'能力较差'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList3")).SelectedValue=="差")
{
sqlstr3="'能力差' = @num3";
selstr3="'能力差'";
} if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="好")
{
sqlstr4="'廉洁好' = @num4";
selstr4="'廉洁好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="比较好")
{
sqlstr4="'廉洁较好' = @num4";
selstr4="'廉洁较好'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="一般")
{
sqlstr4="'廉洁一般' = @num4";
selstr4="'廉洁一般'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="比较差")
{
sqlstr4="'廉洁较差' = @num4";
selstr4="'廉洁较差'";
}
else if(((RadioButtonList)Mydg.FindControl("RadioButtonList4")).SelectedValue=="差")
{
sqlstr4="'廉洁差' = @num4";
selstr4="'廉洁差'";
} if(sqlstr1!="")
{
sqlstr+=sqlstr1;
fir=true;
}
if(sqlstr2!="")
{
if(fir)
{
sqlstr+=",";
sqlstr+=sqlstr2;
}
else
{
sqlstr+=sqlstr2;
}
sec=true;
}
if(sqlstr3!="")
{
if(fir||sec)
{
sqlstr+=",";
sqlstr+=sqlstr3;
}
else
{
sqlstr+=sqlstr3;
}
thir=true;
}
if(sqlstr4!="")
{
if(fir||sec||thir)
{
sqlstr+=",";
sqlstr+=sqlstr4;
}
else
{
sqlstr+=sqlstr4;
}
forth=true;
} if(fir)
{
selstr+=selstr1;
}
else
{
selstr+="'思想一般'";
}
if(sec)
{
selstr+=",";
selstr+=selstr2;
}
else
{
selstr+=",'作风一般'";
}
if(thir)
{
selstr+=",";
selstr+=selstr3;
}
else
{
selstr+=",'能力一般'";
}
if(forth)
{
selstr+=",";
selstr+=selstr4;
}
else
{
selstr+=",'廉洁一般'";
}
selstr+=" WHERE '编号'=@indx1";
cmdsel.CommandText=selstr;
cmdsel.Parameters.Add(new SqlParameter("@indx1",SqlDbType.Int,4));
cmdsel.Parameters["@indx1"].Value=index;
sqlConnection1.Open();
SqlDataReader dr;
dr=cmdsel.ExecuteReader();
sqlstr+=" WHERE '编号'=@indx2";
cmdupdate.CommandText=sqlstr;
cmdupdate.Parameters.Add(new SqlParameter("@indx2",SqlDbType.Int,4));
cmdupdate.Parameters["@indx2"].Value=index;
if(dr.Read())
{
if(fir)
{
thenum1=dr.GetInt16(0);
cmdupdate.Parameters.Add(new SqlParameter("@num1",SqlDbType.Int,16));
cmdupdate.Parameters["@num1"].Value=++thenum1;
}
if(sec)
{
thenum2=dr.GetInt16(1);
cmdupdate.Parameters.Add(new SqlParameter("@num2",SqlDbType.Int,16));
cmdupdate.Parameters["@num2"].Value=++thenum2;
}
if(thir)
{
thenum3=dr.GetInt16(2);
cmdupdate.Parameters.Add(new SqlParameter("@num3",SqlDbType.Int,16));
cmdupdate.Parameters["@num3"].Value=++thenum3;
}
if(forth)
{
thenum4=dr.GetInt16(3);
cmdupdate.Parameters.Add(new SqlParameter("@num4",SqlDbType.Int,16));
cmdupdate.Parameters["@num4"].Value=++thenum4;
}
}
dr.Close();
sqlConnection1.Close();
if(fir||sec||thir||forth)
{
sqlConnection1.Open();
cmdupdate.ExecuteNonQuery();
sqlConnection1.Close();
}
index++;
}
} }
}
而你的dataset2中没有这个字段。解决的方法是,在查询语句中加上这个字段。
为什么会有:将 varchar 值 '编号' 转换为数据类型为 int这个情况啊?难道是编号这里加了单引号?
但是如果没有加单引号,就会出现:'编号'列无效的错误.真是郁闷啊.为什么?高手们真的要来救救我了,我要疯了!!!
能帮我解决这个问题的人怎么还没有出现啊?
cmdupdate.ExecuteNonQuery();看cmdupdate的commmodText是什么,然后取到查询分析器里面,看什么字段(列名没有)
回上面两位,我在DATASET里面已经选了所有的字段了,应该不不可能出现字段没有的情况吧。