【问题标题】:Error in asp.net c# code (mysql database connection)asp.net c#代码中的错误(mysql数据库连接)
【发布时间】:2010-11-24 06:33:35
【问题描述】:

我有一个代码,其中

  1. 我将下拉列表绑定到数据库 之后,

  2. on button click 我连接到 数据库中获取一些值 标签。

我的第一部分工作正常,但是当我尝试做第二部分时,我收到错误消息

在 System.Data.SqlClient.SqlInternalConnection.OnError 的 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔 breakConnection)(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.ConsumeMetaData() 在 System.Data.SqlClient.SqlDataReader.get_MetaData( ) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds 的 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient (CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) .SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String m方法,DbAsyncResult 结果)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,String 方法)在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)在 System.Data。 c:\Documents and Settings\a\My Documents\Visual Studio 2008\WebSites\toolbar1\Default.aspx.cs:line 56 中 _Default.Button1_Click(Object sender, EventArgs e) 处的 SqlClient.SqlCommand.ExecuteReader()

我的代码是:

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

    }
}

请帮我解决我的问题。

【问题讨论】:

    标签: c# asp.net sql mysql


    【解决方案1】:

    你说它是一个 MySQL 数据库.. 你给的连接字符串是"Server=localhost;Database=testcase;Integrated Security=SSPI"

    据我所知.. mysql 连接字符串具有端口 3306 和其他格式。

    查看http://www.connectionstrings.com/ 了解各种数据库的详细连接字符串。

    另外,我假设您确定 MySQL 服务器正在您的计算机上运行 - 通常是 mysqld

    【讨论】:

    • 谢谢,你说的是正确的,而不是使用相同的连接字符串,我的第一部分工作正常吗?
    • 你的第一部分是什么?错误在第 56 行(正如您的堆栈跟踪所指出的那样)。我认为它指向 conn.Open...
    • 我将我的第一部分称为,将下拉列表连接到数据库..这绝对没问题。
    【解决方案2】:

    我猜这是因为您在将参数附加到命令对象之前执行阅读器。试试这个

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

    【讨论】:

    • 你能不能也发布错误信息,我的意思是 exw.Message 而不是 exw.StackTrace
    【解决方案3】:

    你是在增加价值之前执行的。

    --编辑--

    在您更新问题之后。

    试试看:

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

    【讨论】:

    • 错误 1 ​​'System.Data.SqlDbType' 不包含 'String' 的定义
    • 错误 2 当前上下文中不存在名称“customerID”
    【解决方案4】:

    看起来 MySql 不喜欢“SSPI”或“sspi”。 我尝试了“真实”并且它有效。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-11
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2015-04-23
      相关资源
      最近更新 更多