我是C#编程的新手,并尝试使用C#和MySQL数据库在WPF中创建登录表单。当我运行我的WPF并尝试插入数据时,我在这一行上收到错误:
using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username"))
{
cmd.Parameters.AddWithValue("@username", username);
salt = cmd.ExecuteScalar() as string;
}
错误是连接必须有效并且打开。你有什么线索可以解决问题吗?
伙计这是整个代码,我只是用不真实的方式替换敏感数据字段,虽然我改变了我仍然得到错误的东西。你能确定问题的来源吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;
using System.Security.Authentication;
using System.Security.Permissions;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Security.Principal;
using System.Security.Util;
namespace ECBSRecruitmentAgencySoftware
{
public partial class LogIn : Form
{
public LogIn()
{
InitializeComponent();
}
static byte[] GenerateSaltedHash(string plainText, string salt)
{
HashAlgorithm algorithm = new SHA256Managed();
byte[] plainTextBytes = System.Text.Encoding.Unicode.GetBytes(plainText);
byte[] saltBytes = Convert.FromBase64String(salt);
byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
saltBytes.CopyTo(plainTextWithSaltBytes, 0);
plainTextBytes.CopyTo(plainTextWithSaltBytes, salt.Length);
byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);
return hash;
}
public bool tryLogin(string username , string password)
{
using (var con = new MySqlConnection("host=tara.rdb.superhosting.bg;user=sozopouk;password=27051996;database=sozopouk_test2;"))
{
con.Open();
var salt = string.Empty;
using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username"))
{
cmd.Parameters.AddWithValue("@username", username);
salt = cmd.ExecuteScalar() as string;
}
if (string.IsNullOrEmpty(salt)) return false;
var hashedPassword = GenerateSaltedHash(password, salt);
using (var cmd = new MySqlCommand("Select * FROM niki WHERE user_name = @username and user_password = @password"))
{
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", hashedPassword);
using (var reader = cmd.ExecuteReader())
{
return reader.Read();
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (tryLogin(user.Text, pass.Text) == true)
{
MainScreen F2 = new MainScreen();
F2.Show();
this.Hide();
}
else MessageBox.Show("Wrong details!");
}
}
}
你的意思是我必须更新:
using (var con = new MySqlConnection("host=tara.rdb.superhosting.bg;user=sozopouk;password=27051996;database=sozopouk_test2;"))
{
con.Open();
var salt = string.Empty;
using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username"))
{
cmd.Parameters.AddWithValue("@username", username);
salt = cmd.ExecuteScalar() as string;
}
enter code here
enter code here
使用您生成的代码?如果是,我如何实际连接到我的MySQL数据库?
答案 0 :(得分:1)
好吧,在尝试读取或写入数据库之前,您应该打开一个连接,然后发出命令。
using (MySqlConnection cn = GetConnection())
{
cn.Open();
// create the command and link it to the connection
using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username", cn))
{
cmd.Parameters.AddWithValue("@username", username);
salt = cmd.ExecuteScalar() as string;
}
}
public MySqlConnection GetConnection()
{
MySqlConnection cn = new MySqlConnection("Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;");
return cn;
}
编辑:这个难题的缺失部分在这里:
MySqlCommand cmd = MySqlCommand("your_query_text", cn)
答案 1 :(得分:0)
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
using (MySqlCommand command = new MySqlCommand("Select salt From niki where user_name = @username", connection))
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@username", username);
salt = cmd.ExecuteScalar() as string;
}
}
使用您未使用连接的更新帖子编辑,您需要在MySqlCommand的构造函数中
using (MySqlCommand command = new MySqlCommand(Command, Connection))
{
}
或者像
之类的东西command.Connection = connection;
编辑2 如果您使用以下代码替换数据库代码,您是否还有问题?
using (var con = new MySqlConnection("host=tara.rdb.superhosting.bg;user=sozopouk;password=27051996;database=sozopouk_test2;"))
{
con.Open();
var salt = string.Empty;
using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username", con))
{
cmd.Parameters.AddWithValue("@username", username);
salt = cmd.ExecuteScalar() as string;
}
if (string.IsNullOrEmpty(salt)) return false;
var hashedPassword = GenerateSaltedHash(password, salt);
using (var cmd = new MySqlCommand("Select * FROM niki WHERE user_name = @username and user_password = @password", con))
{
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", hashedPassword);
using (var reader = cmd.ExecuteReader())
{
return reader.Read();
}
}
}
答案 2 :(得分:0)
这里有几个问题。
首先,您似乎没有实际实例化MySqlConnection对象,这是建立连接所必需的。
请注意:http://zetcode.com/db/mysqlcsharptutorial/
有关如何在C#中使用MySql的一些信息