Mysql 连接必须有效且打开
连接必须有效且打开。问题出在哪里。net Frmework版本2.0 连接必须有效且打开。问题出在哪里。net Frmework版本2.0 连接必须有效且打开。问题出在哪里。net Frmework版本2.0Mysql 连接必须有效且打开,mysql,connection,Mysql,Connection,连接必须有效且打开。问题出在哪里。net Frmework版本2.0 连接必须有效且打开。问题出在哪里。net Frmework版本2.0 连接必须有效且打开。问题出在哪里。net Frmework版本2.0 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using Sy
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using MySql.Data.MySqlClient;
namespace Student_Portal_Password
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string GetMd5Hash(string input)
{
MD5 md5Hash = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
public void check()
{
if (txtid.Text == "")
{
MessageBox.Show("Please enter Student ID ", MessageBoxIcon.Warning.ToString(), MessageBoxButtons.OK);
}
else if (txtpassword.Text == "")
{
MessageBox.Show("Please enter new password", MessageBoxIcon.Warning.ToString(), MessageBoxButtons.OK);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//check();
txtpassword.Text.Trim();
string hash = GetMd5Hash(txtpassword.Text);
string db = "server=localhost;uid=root;password=usbw;database=dum;";
MySqlConnection dbcon = new MySqlConnection(db);
MySqlCommand cmd = new MySqlCommand(db);
dbcon.Open();
cmd.CommandText = "SELECT * FROM members;";
cmd.ExecuteNonQuery();
MessageBox.Show("Success!");
dbcon.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void txtid_KeyPress(object sender, KeyPressEventArgs e)
{
const char Delete = (char)8;
e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
使用System.Security.Cryptography;
使用MySql.Data.MySqlClient;
名称空间学生\门户\密码
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
公共静态字符串GetMd5Hash(字符串输入)
{
MD5 md5Hash=MD5.Create();
//将输入字符串转换为字节数组并计算哈希。
byte[]data=md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
//创建新的Stringbuilder以收集字节
//并创建一个字符串。
StringBuilder sBuilder=新StringBuilder();
//循环遍历散列数据的每个字节
//并将每个字符串格式化为十六进制字符串。
for(int i=0;i
几件事
您正在使用ExecuteNonQuery
进行查询。请尝试使用例如ExecuteReader
您没有为DbCommand设置连接,因此执行该命令将找不到数据库。试试这个
MySqlConnection dbcon = new MySqlConnection(db);
string sql = "SELECT * FROM members";
MySqlCommand cmd = new MySqlCommand(sql, dbcon); //<-- pass connection to command
…将连接字符串作为要执行的SQL传入,并且不将该命令与任何数据库连接相关联。这将给出您正在询问的错误。是否有我没有看到的问题?这是否有效
代码>?从成员中选择*;指向收到此消息的代码行。这将帮助人们缩小他们的注意力。请不要只是为了绕过内容过滤器而重复句子。他们在那里是有原因的。
MySqlCommand cmd = new MySqlCommand(db);