gpt4 book ai didi

c# - asp.net c#代码中的错误(mysql数据库连接)

转载 作者:行者123 更新时间:2023-11-29 14:57:21 25 4
gpt4 key购买 nike

我有一个代码,其中

  1. 我将下拉列表绑定(bind)到数据库再后来,

  2. 点击按钮我连接到 数据库中获取一些值 标签。

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

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 method, 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

我的代码是:

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 服务器正在您的计算机上运行 - 它通常是 mysqld

关于c# - asp.net c#代码中的错误(mysql数据库连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4264058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com