2010-11-24 84 views
0

我具有其中錯誤在asp.net C#代碼(MySQL數據庫連接)

  1. I A下拉列表綁定到數據庫 和稍後我連接到 一個代碼,

  2. on button click數據庫以獲得 標籤中的某個值。

我的第1部分工作正常,但當我嘗試做第二部分我得到的錯誤消息

在System.Data.SqlClient.SqlConnection.OnError(SqlException異常,布爾breakConnection) System.Data.SqlClient.SqlInternalConnection.OnError(SqlException異常,布爾breakConnection)在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream ,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)在System.Data.SqlClient.SqlDataReader.ConsumeMetaD System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior)上System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)System.Data.SqlClient.SqlDataReader.get_MetaData()ata (CommandBehavior cmdBehavior,RunBehavior runBehavior,布爾returnStream,String方法,DbAsyncResult結果)在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布爾異步)在System.Data.SqlClient.SqlCommand.RunExecuteReader ,布爾returnStream,字符串方法)在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior行爲,字符串方法)在System.Data.SqlClient.SqlCommand.ExecuteReader()在_Default.Button1_Click(對象發件人,EventArgs e)在c: \ Documents and Settings \ a \ My Documents \ Visual Studio 2008 \ WebSites \ toolbar1 \ Default.aspx.cs:line 56

我的代碼是:

using System; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 

using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      try 
      { 
       SqlConnection myConn = new SqlConnection("Server=localhost;Database=testcase;Integrated Security=SSPI"); 
       SqlCommand myCmd = new SqlCommand("select skey,casecode from casetype", myConn); 
       myConn.Open(); 
       SqlDataReader myReader = myCmd.ExecuteReader(); 

       //Set up the data binding. 
       DropDownList3.DataSource = myReader; 
       DropDownList3.DataTextField = "skey"; 
       DropDownList3.DataValueField = "casecode"; 
       DropDownList3.DataBind(); 

       //Close the connection. 
       //myConn.Close(); 
       //myReader.Close(); 

       //Add the item at the first position. 
       DropDownList3.Items.Insert(0, "<-- Select -->"); 

      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.StackTrace); 
      } 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      SqlConnection myConn1 = new SqlConnection("Server=localhost;Database=testcase;Integrated Security=SSPI"); 
      SqlCommand myCmd1 = new SqlCommand("select casename,skey from casetype where skey=?", myConn1); 
      myConn1.Open(); 
      SqlDataReader myReader1 = myCmd1.ExecuteReader(); 
      String type = DropDownList3.SelectedItem.Text; 
      myCmd1.Parameters.AddWithValue("?", type); 
     } 
     catch (Exception exw) 
     { 
      Response.Write(exw.StackTrace); 
     } 

    } 
} 

請幫我解決我的問題。

回答

1

你是說這是一個MySQL數據庫..那你給連接字符串"Server=localhost;Database=testcase;Integrated Security=SSPI"

據我所知.. MySQL連接字符串有3306端口和其他一些格式。

查看http://www.connectionstrings.com/瞭解各種數據庫的詳細連接字符串。

此外,我假設您確定MySQL服務器正在您的計算機上運行 - 通常是mysqld

+0

謝謝你,你說的是正確的比如何用相同的連接字符串我的第一部分工作正常嗎? – Ishan 2010-11-24 06:45:58

+0

你的第一部分是什麼?錯誤位於第56行(由堆棧跟蹤指出)。我認爲它指向conn.Open ... – Sekhar 2010-11-24 06:46:37

+0

我把我的第一部分稱爲,將dropdownlist連接到數據庫..這工作絕對好。 – Ishan 2010-11-24 06:53:22

0

我想這是因爲您在將參數附加到命令對象之前正在執行讀取器。試試這個

protected void Button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      SqlConnection myConn1 = new SqlConnection("Server=localhost;Database=testcase;Integrated Security=SSPI"); 
      SqlCommand myCmd1 = new SqlCommand("select casename,skey from casetype where skey=?", myConn1); 
      myConn1.Open(); 
String type = DropDownList3.SelectedItem.Text; 
      myCmd1.Parameters.AddWithValue("?", type); 
      SqlDataReader myReader1 = myCmd1.ExecuteReader(); 

     } 
     catch (Exception exw) 
     { 
      Response.Write(exw.StackTrace); 
     } 

    } 
0

您正在執行添加值之前。

- 編輯 -

更新的問題後。

嘗試,以下操作:

string type = DropDownList3.Items[DropDownList3.SelectedIndex].Text; 
string commandText = "select casename,skey from casetype where [email protected];"; 
using (SqlConnection connection = new SqlConnection("Server=localhost;Database=testcase;Integrated Security=SSPI")) 
{ 
    SqlCommand command = new SqlCommand(commandText, connection); 
    command.Parameters.Add("@key", SqlDbType.Text); //Same as System.String 
    command.Parameters["@key"].Value = type ; //Value from your text box! 
    //command.Parameters.AddWithValue("@key", type); 

    try 
    { 
     connection.Open(); 
     Int32 rowsAffected = command.ExecuteNonQuery(); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 

} 
0

看起來像MySQL不喜歡 「SSPI」 或 「SSPI」。 我試過「真」,它的工作原理。

<add name="ConnStr" providerName="MySql.Data.MySqlClient" 
    connectionString="server=localhost;port=3306;database=myDb;Integrated Security=true;"/>