3
  DataSet ds = new DataSet();

    SqlDataAdapter cmd = new SqlDataAdapter("Select * FROM event", con);
    cmd.Fill(ds, "Table");

一次又一次地得到同样的错误尝试了一切

请帮忙

4

2 回答 2

4

编辑

像这样尝试,其中缺少的元素可能是在下面的代码中添加的命令类型,并且还为数据库打开连接

  SqlConnection sqlcon;
   using(sqlcon = new SqlConnection("Data Source=Servername;Initial 
       Catalog=Marketing;Integrated Security=SSPI"))
   {

        SqlCommand cmd = new SqlCommand("Select * FROM event", sqlcon);
        cmd.CommandType = CommandType.Text;
        sqlcon.Open();
        da = new SqlDataAdapter(sqlcmd);
        da.Fill(dt);
   }

注意:在 using 语句中使用然后处理所有内容。这也是在上面添加的


在调用填充方法之前执行此操作

SqlConnection conn= new 
         SqlConnection("Data Source=Servername;Initial Catalog=Marketing;Integrated Security=SSPI");

SqlDataAdapter cmd = new SqlDataAdapter("Select * FROM event", con);
   cmd.Fill(ds, "Table"); 
于 2012-09-21T07:38:20.710 回答
0
SqlConnection myconn = new SqlConnection(YourConnString);
SqlCommand mycmd = new SqlCommand();

try{ myconn.Open(); }
catch (Exception ex) { MessageBox("Error");}
mycmd.Connection = myconn;

DataSet ds = new DataSet();
DataTable dt = new DataTable();

String sqlStr = "Select * FROM event";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, myconn);
da.Fill(ds);
dt = ds.Tables[0];
于 2012-09-21T09:10:06.577 回答