我已在本地计算机上安装MySQL,并尝试允许将数据从Visual Studio 2017上的ASP.NET Web应用程序插入到已创建的MySQL表中。我设法成功建立了MySQL连接并创建了表称为“事件”。
最初,使用下面的代码,我没有任何语法或系统错误,但是每当我单击并尝试从Web表单插入数据时,就不会将任何数据插入MySQL表中。如果我的代码或任何其他文件配置有任何错误的任何建议?
后来,在尝试安装.NET连接器后,我仍然无法将数据插入MySql,并且在MySqlConnection命名空间中出现以下错误,指出“ -type-mysqlconnection-exists-in-both-mysql-data-issue”如下所示
有什么建议可以纠正,以便能够将数据插入表中?是在我的插入代码中还是问题出在其他地方?下面是我的代码:
AddEvent.aspx.cs:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace Prototype3
{
public partial class AddEvent : System.Web.UI.Page
{
//convert event date input into DateTime format on back-end of page
string dateStr = dateinput1.Value;
DateTime date;
//Saving data on added event into MySQL database
string constring = "server=localhost;user id=***********;
password=***********!;
persistsecurityinfo=True;database=homesecretary";
//Connection to database below-check!!
string Query = "INSERT into events (eventTitle) VALUES
('"+tb_eventTitle.Text+"')"; //Parse date input from jQuery
datepicker
MySqlConnection connDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, connDataBase);
MySqlDataReader myReader;
try
{
connDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
connDataBase.Close();
}
Web.config:
<configuration>
<connectionStrings>
<add name="homesecretaryConnectionString"
connectionString="server=localhost;user
id=********; password=********; persistsecurityinfo=True;
database=homesecretary" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
.....
</configuration>
答案 0 :(得分:1)
您在这里犯了一个非常基本/根本的错误。
MySqlDataReader的目的是在我们选择数据时使用。您需要插入数据而不查询,因此它应该调用ExecuteNonQuery()
方法:
// for inserts/updates queries
cmdDataBase.ExecuteNonQuery()
请参阅以下文档,以获取有关如何在ADO.NET中插入数据的示例:
MSDN - Insert New Records in to Database
注意:请不要对创建查询进行字符串连接,因为这不是一个好习惯,而是应该使用参数化查询。 See an example here
答案 1 :(得分:0)
要update/insert
,您可能需要使用ExecuteNonQuery
并填充DataSet
,您可以使用MySqlDataAdapter
,如下所示:
private static string _DBConnectionString = string.Empty;
public static int SetData(string sqlQuery)
{
int iReturn = -1;
try
{
MySqlCommand cmd = new MySqlCommand(sqlQuery);
using (MySqlConnection conn = new MySqlConnection(DBConnectionString))
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
iReturn = cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
catch (Exception E)
{
iReturn = -1;
}
return iReturn;
}
public static object GetData(string sqlQuery)
{
DataSet dtSet = new DataSet();
try
{
MySqlCommand cmd = new MySqlCommand(sqlQuery);
MySqlDataAdapter adpt = new MySqlDataAdapter();
using (MySqlConnection conn = new MySqlConnection(DBConnectionString))
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.CommandTimeout = 0;
adpt.SelectCommand = cmd;
cmd.Connection = conn;
adpt.Fill(dtSet);
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
catch (Exception E)
{
}
return dtSet;
}