简体   繁体   English

asp.net C#代码错误(MySQL数据库连接)

[英]Error in asp.net c# code (mysql database connection)

I have a code in which 我有一个代码

  1. i bind a dropdownlist to a database and later on , 我将一个下拉列表绑定到数据库,然后,

  2. on button click i connect to database to get some value in a label. on button click我连接到数据库”以获得标签中的某些值。

My 1st part works fine but when i try to do the second part i get the error message as 我的第一部分工作正常,但是当我尝试执行第二部分时,我收到了错误消息

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String meth 在系统上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 result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader() at _Default.Button1_Click(Object sender, EventArgs e) in c:\\Documents and Settings\\a\\My Documents\\Visual Studio 2008\\WebSites\\toolbar1\\Default.aspx.cs:line 56 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)

My code is: 我的代码是:

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);
        }

    }
}

Please help me to solve my problem. 请帮助我解决我的问题。

You are saying it's a MySQL database .. and the connection string that you gave is "Server=localhost;Database=testcase;Integrated Security=SSPI" 您说的是一个MySQL数据库..,您提供的连接字符串是"Server=localhost;Database=testcase;Integrated Security=SSPI"

As far as I'm aware.. mysql connection strings have port 3306 and some other format. 据我所知.. mysql连接字符串具有端口3306和其他一些格式。

Have a look at http://www.connectionstrings.com/ for detailed connection strings for various databases. 有关各种数据库的详细连接字符串,请访问http://www.connectionstrings.com/

Also, I am assuming that you are sure that the MySQL Server is running on your computer - it is usually mysqld . 另外,我假设您确定MySQL Server在计算机上运行-通常为mysqld

Looks like MySql does not like "SSPI" or "sspi". 看起来MySql不喜欢“ SSPI”或“ sspi”。 I tried "true" and it works. 我尝试过“ true”,它可以工作。

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

I guess this is because you are executing reader before attaching paramater to command object. 我猜这是因为您在将参数附加到命令对象之前正在执行读取器。 Try this 尝试这个

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); 
        } 

    } 

You are executing before adding value. 您正在执行增值之前。

--EDIT-- - 编辑 -

After your updated question. 在您更新问题之后。

Try, following: 尝试以下操作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM