[英]Error in asp.net c# code (mysql database connection)
我有一個代碼
我將一個下拉列表綁定到數據庫,然后,
on button click
我連接到數據庫”以獲得標簽中的某些值。
我的第一部分工作正常,但是當我嘗試執行第二部分時,我收到了錯誤消息
在系統上System.Data.SqlClient.SqlInternalConnection.OnError(SqlException異常,布爾breakConnection)在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)在System.Data.SqlClient.SqlConnection.OnError(SqlException異常,Boolean breakConnection) .Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()在System.Data.SqlClient.SqlDataReader.get_ .Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)在System.Data.SqlClient處位於System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,布爾異步)。 RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String meth od,DbAsyncResult結果)位於System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior行為,String方法)位於System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法) SqlClient.SqlCommand.ExecuteReader()位於c:\\ Documents and Settings \\ a \\ My Documents \\ Visual Studio 2008 \\ WebSites \\ toolbar1 \\ Default.aspx.cs:第56行中的_Default.Button1_Click(Object sender,EventArgs e)
我的代碼是:
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);
}
}
}
請幫助我解決我的問題。
您說的是一個MySQL數據庫..,您提供的連接字符串是"Server=localhost;Database=testcase;Integrated Security=SSPI"
據我所知.. mysql連接字符串具有端口3306和其他一些格式。
有關各種數據庫的詳細連接字符串,請訪問http://www.connectionstrings.com/ 。
另外,我假設您確定MySQL Server在計算機上運行-通常為mysqld
。
看起來MySql不喜歡“ SSPI”或“ sspi”。 我嘗試過“ true”,它可以工作。
<add name="ConnStr" providerName="MySql.Data.MySqlClient"
connectionString="server=localhost;port=3306;database=myDb;Integrated Security=true;"/>
我猜這是因為您在將參數附加到命令對象之前正在執行讀取器。 嘗試這個
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);
}
}
您正在執行增值之前。
- 編輯 -
在您更新問題之后。
嘗試以下操作:
string type = DropDownList3.Items[DropDownList3.SelectedIndex].Text;
string commandText = "select casename,skey from casetype where skey=@key;";
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.