【问题标题】:How to resolve Visual Studio MYSQL exception?如何解决 Visual Studio MYSQL 异常?
【发布时间】:2019-09-12 02:18:04
【问题描述】:

我试图将数据插入到我的本地主机数据库 (xampp phpmyadmin),但我一直收到此错误。我尝试查找异常,但未能找到任何答案。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ProductMaintenance
{
    public partial class Form1 : Form
    {
        SqlConnection connection = new SqlConnection("server=localhost;user id=root123;database=test;allowuservariables=True;persistsecurityinfo=True");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            connection.Open();

            SqlCommand cmd = connection.CreateCommand();

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "insert into [mytable] (Name,Surname,Address) values (' "+ textBox1.Text + " ', '" + textBox2.Text + " ', ' " + textBox3 + "')";

            cmd.ExecuteNonQuery();
            connection.Close();

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";

        }

【问题讨论】:

标签: c#


【解决方案1】:

您正在使用 Microsoft SQLServer 的 .Net 类连接到 MySQL 服务器。由于 'allowuservariables' 仅受 MySql 支持,因此 SQLServer Connection 类会引发您的错误。查看MySqlConnector NuGet-Package - 仅更改连接字符串将无法连接到您的 MySql 实例!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多