小弟刚学C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(@"Data Source =.\SQLEXPRESS;AttachDBFilename=C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\DB_DataBase.mdf;Integrated Security=True;
User Instance=true;"))
{ // string sql = "SELECT name FROM SysColumns WHERE SysColumns.id=object_id('" + comboBox1.Text + "')";
//string sql="SELECT * FROM "+comboBox1.Text;
string table=comboBox1.Text;
string sql = string.Format("SELECT * FROM {0}",table);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
ada.Fill(ds,"table");
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
}
提示说:对象名 'System.Data.DataRowView' 无效。
怎么改代码……
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(@"Data Source =.\SQLEXPRESS;AttachDBFilename=C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\DB_DataBase.mdf;Integrated Security=True;
User Instance=true;"))
{ // string sql = "SELECT name FROM SysColumns WHERE SysColumns.id=object_id('" + comboBox1.Text + "')";
//string sql="SELECT * FROM "+comboBox1.Text;
string table=comboBox1.Text;
string sql = string.Format("SELECT * FROM {0}",table);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
ada.Fill(ds,"table");
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
}
提示说:对象名 'System.Data.DataRowView' 无效。
怎么改代码……
string table=comboBox1.Text;
string sql = string.Format("SELECT * FROM {0}",table);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
ada.Fill(ds,"table");
dataGridView1.DataSource = ds.Tables[0];
con.Close();
主要是这段代码执行SQL语句时,提示说:对象名 'System.Data.DataRowView' 无效。
string sql = string.Format("SELECT * FROM {0}",table);很少见这样的写法;
直接写string sql="select * from table" ;(table是要查询的表名)
1.对象名 'System.Data.DataRowView' 无效。界面部分有该控件(DataGridView)没有,有的话检查ID是否和这里的一致
2.检查表名
得到的 table 是什么值,值对不对。
也许返回的是其他 数据库中没有的数据。
一般comboBox 有 selectedText 还是 selectedValue 属性 试试看。然后 如果得到 table 有 数据 但是 后面查询 出错。
那么 你试试 直接 将 table 替换 成 数据库中已有的表名 看看是否能读取数据。如果 select * from 表名 仍然 出错 那可能就是 连接字符 或者 打开方式不对了
==>
那你把 sql语句输出来,
在查询分析器里执行看看,能不能成功
在查询分析器里好找 sql 语句的错误