//一段查询的代码,先输入景点的名称,然后再下拉框中选择条件,然后弹出结果,但是老实报“对象名Attractions无效”,这是怎么回事,请大侠帮忙,数据库中有Attractions这个表
namespace DataConnect
{
public partial class NameSelect : Form
{
public NameSelect()
{
InitializeComponent();
} private void btnNameSelect_Click(object sender, EventArgs e)
{
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 == null && combNS == null )
{
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++)
{
//for (int j = 0; j < 2; j++)
//{
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;
DataSet Ds = new DataSet();
int Row = Da.Fill(Ds, "Attractions");
string bh = Ds.Tables["Attractions"].Rows[Convert.ToInt32(txtNS)][a].ToString();
lblShow.Text = bh;
sqlConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//一段查询的代码,先输入景点的名称,然后再下拉框中选择条件,然后弹出结果,但是老实报“对象名Attractions无效”,这是怎么回事,请大侠帮忙,数据库中有Attractions这个表,请大侠帮帮小弟
namespace DataConnect
{
public partial class NameSelect : Form
{
public NameSelect()
{
InitializeComponent();
} private void btnNameSelect_Click(object sender, EventArgs e)
{
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 == null && combNS == null )
{
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++)
{
//for (int j = 0; j < 2; j++)
//{
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;
DataSet Ds = new DataSet();
int Row = Da.Fill(Ds, "Attractions");
string bh = Ds.Tables["Attractions"].Rows[Convert.ToInt32(txtNS)][a].ToString();
lblShow.Text = bh;
sqlConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//一段查询的代码,先输入景点的名称,然后再下拉框中选择条件,然后弹出结果,但是老实报“对象名Attractions无效”,这是怎么回事,请大侠帮忙,数据库中有Attractions这个表,请大侠帮帮小弟
查询分析器执行
连接字符串
SqlConnection sqlConn = new SqlConnection(conn );
sqlConn.Open();????那有Connection的Text是这样的??"Trusted_Connection=SSPI";
sqlstr = "select Kind ,NF from Attractions where Kind = '"+txtNS+"' and a = '"+combNS+"'";1楼的大神已经说的很明显了,自己不仔细检查~ 你的字符串是写死的~~ 要用活的 就要'"+txtNS+"'
还有,你的and a = …… 你这个a 是怎么回事? 你的数据库里有 a 这个字段吗?
你还是先看看书吧。