我在将 c# 与 mysql 数据库连接时遇到了一个小问题。当我尝试从文本框中插入一个值时,我收到以下错误。我想知道是否有人可以帮助我/向我解释我做错了什么。
这是我得到的错误:“字段列表”中的未知列“测试”
这是我连接数据库的代码:
namespace Planner
{
internal class DBConnect
{
private MySqlConnection _connection = new MySqlConnection();
private string _server;
private string _database;
private string _uid;
private string _password;
//private string _port;
//private bool succes = false;
//Constructor
public DBConnect()
{
Initialize();
}
//Initialize values
public void Initialize()
{
string connectionString2 = "Server=localhost;Port=3307;Database=test;Uid=root;Pwd=usbw";
//+ "Port:" + _port + ";"
_server = "localhost:3307";
//_port = "3307";
_database = "test";
_uid = "root";
_password = "usbw";
string connectionString = "Server=" + _server + ";" + "Database=" +
_database + ";" + "Uid=" + _uid + ";" + "Pwd=" + _password + ";";
_connection = new MySqlConnection(connectionString2);
}
public bool OpenConnection()
{
try
{
_connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server");
break;
case 1042:
MessageBox.Show("Unable to connect to any of the specified MySQL hosts");
break;
case 1045:
MessageBox.Show("Invalid username/password");
break;
}
return false;
}
}
private List<string>[] Select()
{
string selectquery = "SELECT * FROM tabelname";
List<string>[] selectlist = new List<string>[3];
selectlist[0] = new List<string>();
selectlist[1] = new List<string>();
selectlist[2] = new List<string>();
MySqlCommand cmd = new MySqlCommand(selectquery, _connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
selectlist[0].Add(dataReader["waarde"] + "");
selectlist[1].Add(dataReader["waarde"] + "");
selectlist[2].Add(dataReader["waarde"] + "");
}
dataReader.Close();
return selectlist;
}
public void Insert(string textvalue)
{
string insertquery = "INSERT INTO testconnectie(text) VALUES ("+textvalue+")";
MySqlCommand cmd = new MySqlCommand(insertquery, _connection);
cmd.ExecuteNonQuery();
}
private void Update()
{
string updatequery = "UPDATE tabelnaam SET waarde='', waarde'' WHERE waarde=''";
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = updatequery;
cmd.Connection = _connection;
cmd.ExecuteNonQuery();
}
private void Delete()
{
string deletequery = "DELETE FROM tabelnaam WHERE waarde=''";
MySqlCommand cmd = new MySqlCommand(deletequery, _connection);
cmd.ExecuteNonQuery();
}
public bool CloseConnection()
{
try
{
_connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public void Backup()
{
try
{
DateTime Time = DateTime.Now;
int year = Time.Year;
int month = Time.Month;
int day = Time.Day;
int hour = Time.Hour;
int minute = Time.Minute;
int second = Time.Second;
int millisecond = Time.Millisecond;
//Save file to C:\ with the current date as a filename
string path;
path = "C:\\ChatBackup" + year + "-" + month + "-" + day +
"-" + hour + "-" + minute + "-" + second + "-" + millisecond + ".sql";
StreamWriter file = new StreamWriter(path);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "Database Backup";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
_uid, _password, _server, _database);
psi.UseShellExecute = false;
Process process = Process.Start(psi);
string output;
output = process.StandardOutput.ReadToEnd();
file.WriteLine(output);
process.WaitForExit();
file.Close();
process.Close();
}
catch (IOException ex)
{
MessageBox.Show("Error , unable to backup! " + ex);
}
}
}
}
主窗体
namespace Planner
{
public partial class MainForm : Form
{
private DBConnect mysql = new DBConnect();
public MainForm()
{
InitializeComponent();
mysql.Initialize();
mysql.OpenConnection();
}
private void _sendMessageButton_Click(object sender, System.EventArgs e)
{
string textvalue = _messageTextBox.Text;
mysql.Insert(textvalue);
}
}
}
有人可以向我解释我做错了什么,在此先感谢。