mysql “无法从db_shoes.Koneksi转换为字符串”

mkshixfv  于 2023-04-10  发布在  Mysql
关注(0)|答案(1)|浏览(87)
MySqlConnection dbcon = new MySqlConnection(koneksi2);

MySqlCommand command = new MySqlCommand(query,dbcon);
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@address", address);
command.Parameters.AddWithValue("@Telp", telp);
command.Parameters.AddWithValue("@mail", mail);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
int count = command.ExecuteNonQuery();
if (count > 0)
{
    MessageBox.Show("Your Account Has Been Succsess Added");
    // reset textbox
    txtName.Text = "";
    txtAddress.Text = "";
    txtTelp.Text = "";
    txtMail.Text = "";
    txtUsername.Text = "";
    txtPassword.Text = "";
}
else
{
    MessageBox.Show("Failed to create your new account, Please try again!!");
}

此语法的错误是=“无法从db_shoes.Koneksi转换为字符串”
你能解释并帮助我这个错误吗?
我试图连接我的数据库,但无法连接。在我运行该文件后,有一些弹出窗口和文本连接必须是有效的和开放的。之后,错误已经显示在我的代码
使用C#的代码
谢谢

twh00eeo

twh00eeo1#

看一下我用来向数据库dbName添加数据的示例代码

public static string dbName = "AFriends.db";

这是声明为顶级的。看看这个代码结构并注意USING

public void saveData()
    {
        using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{dbName}';Version=3;"))
        {
            conn.Open();
            string sql = "INSERT INTO FriendsData " +
                        "   (fxFirstName, fxLastName, fxAddress, fxCity, fxState, fxZip, fxCellPhone, fxEmail, fxInfo) " +
                        "   VALUES " +
                        "   (@fxFirstName, @fxLastName, @fxAddress, @fxCity, @fxState, @fxZip, @fxCellPhone, @fxEmail, @fxInfo)";

            using (var cmd = new SQLiteCommand(sql, conn))
            {
                try
                {
                    cmd.Parameters.AddWithValue("@fxFirstName", fn);
                    cmd.Parameters.AddWithValue("@fxLastName", ln);
                    cmd.Parameters.AddWithValue("@fxAddress", ad);
                    cmd.Parameters.AddWithValue("@fxCity", ct);
                    cmd.Parameters.AddWithValue("@fxState", st);
                    cmd.Parameters.AddWithValue("@fxZip", zp);
                    cmd.Parameters.AddWithValue("@fxCellPhone", ph);
                    cmd.Parameters.AddWithValue("@fxEmail", em);
                    cmd.Parameters.AddWithValue("@fxInfo", rt);

                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    result = ex.ToString();
                    tbMessage.Text = result;
                }
            }
        }
    }

相关问题