Connection must be valid and open. where is the problem ? .net Frmework version 2.0 Connection must be valid and open. where is the problem ? .net Frmework version 2.0 Connection must be valid and open. where is the problem ? .net Frmework version 2.0
连接必须有效且开放。问题出在哪儿 ? .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 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;
}
}
}
1 个解决方案
#1
0
A couple of things;
一些事情;
You're using ExecuteNonQuery
for a Query. Try for example ExecuteReader
instead.
您正在使用ExecuteNonQuery进行查询。请尝试使用示例ExecuteReader。
You're not setting a connection for your DbCommand, so executing it won't find the database.
Try this instead;
您没有为DbCommand设置连接,因此执行它将无法找到数据库。试试这个;
MySqlConnection dbcon = new MySqlConnection(db);
string sql = "SELECT * FROM members";
MySqlCommand cmd = new MySqlCommand(sql, dbcon); //<-- pass connection to command
Your current code;
您当前的代码;
MySqlCommand cmd = new MySqlCommand(db);
...passes in the connection string as the SQL to execute, and does not associate the command with any database connection. That will give the error you're asking about.
...将连接字符串作为要执行的SQL传递,并且不将该命令与任何数据库连接相关联。这会给你提出的错误。
#1
0
A couple of things;
一些事情;
You're using ExecuteNonQuery
for a Query. Try for example ExecuteReader
instead.
您正在使用ExecuteNonQuery进行查询。请尝试使用示例ExecuteReader。
You're not setting a connection for your DbCommand, so executing it won't find the database.
Try this instead;
您没有为DbCommand设置连接,因此执行它将无法找到数据库。试试这个;
MySqlConnection dbcon = new MySqlConnection(db);
string sql = "SELECT * FROM members";
MySqlCommand cmd = new MySqlCommand(sql, dbcon); //<-- pass connection to command
Your current code;
您当前的代码;
MySqlCommand cmd = new MySqlCommand(db);
...passes in the connection string as the SQL to execute, and does not associate the command with any database connection. That will give the error you're asking about.
...将连接字符串作为要执行的SQL传递,并且不将该命令与任何数据库连接相关联。这会给你提出的错误。