2014-06-25 57 views
0

我该如何解决这个问题? 我想弄清楚如何将我的mysql数据传入一个文本框。 另外,我在运行我的程序时发生错误(连接必须有效且打开)。通过ASP.NET的MySQL连接

try 
    { 
     string sCon = "SERVER=localhost;DATABASE=pac;UID=root"; 
     String s = "select * from pac.pac_excel where employeenumber=" + this.employeenumber_txt.Text + ";"; 
     MySqlConnection con = new MySqlConnection(sCon); 
     MySqlCommand myCommand = new MySqlCommand(s, con); 
     MySqlDataAdapter dat = new MySqlDataAdapter(s, con); 
     DataSet ds = new DataSet(); 
     dat.Fill(ds, "pac"); 

     MySqlCommand cmdDatabase = new MySqlCommand(s); 
     MySqlDataReader myReader; 
     GridView1.DataSource = ds.Tables[0]; 
     GridView1.DataBind(); 
     con.Open(); 
     myCommand.ExecuteNonQuery(); 

      myReader = cmdDatabase.ExecuteReader(); 
      while (myReader.Read()) 
      { 
       status_txt.Text = myReader["status"].ToString(); 
      } 
      myReader.Close(); 
      con.Close(); 

    } 

    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
+1

运行此错误时会得到什么错误消息? – Logard

+0

程序正在运行。错误提示“连接必须有效且打开”。它应该是mysql的变量。 – Tryztoff

+0

由于gkrisky说你应该把你的连接打开()移到你的dat.Fill()调用之前。如果这对你有用,请给他信用:) – Logard

回答

0

如果您使用的是SQL Express版本更改SCON这样,

string sCon = "Data Source=localhost\SQLEXPRESS;Initial Catalog=pac;Integrated Security=True;UID=root"; 

,你需要添加\ SQLEXPRESS否则没有必要this.And的更改con.Open()的行低于con声明。

+0

谢谢,但是在声明con后移动con.Open()后仍然会出现相同的错误。 – Tryztoff