SqlCommand Comd = new SqlCommand(sqlstr, sqlConn);
SqlDataAdapter Da = new SqlDataAdapter();
Da.SelectCommand = Comd;
Comd.CommandType = CommandType.Text;
DataSet Ds = new DataSet("Attractions");
Da.Fill(Ds, "Attractions");
DataTable table = new DataTable();
table = Ds.Tables["Attractions"];
第6句代码报异常:“对象名 'Attractions' 无效。”,可是我在数据库的表名称就是Attractions,这是怎么回事??请问是哪里错误???
Da.Fill(Ds, "Attractions");最后ds.close();
Ds.Tables.Add("Attractions");//先加DataTable
Da.Fill(Ds, "Attractions");
sqlstr的sql语句中的表名写错了,可以检查,和datatable无关
string conn = "Trusted_Connection=SSPI";
SqlConnection sqlConn = new SqlConnection(conn );
sqlConn.Open();
string a = String.Empty;
string sqlstr = String.Empty;
string txtNS = txtInputName.Text;
string combNS = String.Empty;
combNS = comboBoxSelectKind.SelectedItem.ToString();
if (txtNS.Length == 0 || combNS.Length == 0 )
{
MessageBox.Show("请输入景点名称");
}
string NF = String.Empty;
string[,] s = new string[,] { { "地质", "tp_Geology" }, { "土壤", "tp_Soil" }, { "植物", "tp_Plant" }, { "气候", "tp_Climate" }, { "地貌", "tp_Relief" } };
for (int i = 0; i < 5; i++)
{
if (combNS == s[i, 0])
{
NF = s[i, 0];
a = s[i, 1];
}
}
sqlstr = "select Kind ,NF from Attractions where Kind = txtNS and a = combNS";
try
{
SqlCommand Comd = new SqlCommand(sqlstr, sqlConn);
SqlDataAdapter Da = new SqlDataAdapter();
Da.SelectCommand = Comd;
Comd.CommandType = CommandType.Text;
DataSet Ds = new DataSet("Attractions");
DataTable table = new DataTable();
table = Ds.Tables.Add ("Attractions");
Da.Fill(Ds, "Attractions");
DataRow row = table.NewRow();
string bh = Ds.Tables["Attractions"].Rows[Convert.ToInt32(txtNS)][a].ToString();
lblShow.Text = bh;
sqlConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}完整的代码是这样的,请各位帮我看看。我主要想实现的功能是:根据用户输入的姓名(txtNS)和下拉列表的选择,得出相应的内容。下拉列表的选项是 地质、地貌、气候、土壤、植物,数据库中对应的字段名称就是s数组中英文的部分,
明显有问题~
然后你的表名也是这个, 会不会 冲突了?
你用查询分析器走下这个语句看看
我感觉是Kind = txtNS and a = combNS 这有问题
SqlConnection sqlConn = new SqlConnection(conn );
sqlConn.Open();请各位帮忙看看
就行了,为啥要用Sqlcommand
试试这个
DataTable table = new DataTable();
table = Ds.Tables.Add ("Attractions");
Da.Fill(Ds, "Attractions");