c#.net中的执行查询问题 [英] executenonquery problem in c#.net
问题描述
我在执行非查询sql语句时遇到问题。请记住,我还是.NET框架和MySQL的新手。这是我的代码。
private void Binding()
{
string connStr =datasource = localhost; port = 3306;用户名=根;密码=根;;
conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
try
{
string database = schemaForm.getData;
dtable = new DataTable();
bindingSource = new BindingSource(); ;
conn.Open();
command.CommandText =SELECT Metabolite_Name+
FROM+ database +
.Metabolites WHERE+
MetaboliteID IN('met1','met2'); ;
command.ExecuteNonQuery();
sqlData.SelectCommand = command;
sqlData.Fill(dtable);
bindingSource.DataSource = dtable;
dbMetName.DataSource = dtable;
dtable.Columns.Add(代谢物名称);
dbMetName.DataSource = dtable;
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
从getData表单传递值
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
命名空间DynamicSimulator_v2
{
public partial class SchemaName:Form
{
private static string data;
public SchemaName()
{
InitializeComponent();
}
private void btnCancel_Click(object sender,EventArgs e)
{
this.Hide();
}
private void btnOK_Click(object sender,EventArgs e)
{
data = txtDB.Text;
this.Hide();
}
公共字符串getData
{
set
{
data = txtDB.Text;
}
获得
{
返回数据;
}
}
}
}
错误显示我的查询是错误。我非常确定查询是正确的,因为我在MySQL脚本上测试它。我试图在datagridview中显示它。 dbMetName是datagridview。
Quote:command.ExecuteNonQuery();
用于插入/更新
您应该使用ExecuteReader并将数据加载到DataTable并最终绑定到Grid。
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(dr);
public partial class登录:表格
{
SqlConnection con = new SqlConnection(@ Data Source = .\SQLEXPRESS; Initial Catalog = Library_management_system; Integrated Security = True; User Instance = True);
int count = 0;
public Login( )
{
InitializeComponent();
}
private void button1_Click (对象发送者,E ventArgs e)
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =select * from Library_person where usernaem ='+ textBox1.Text +'and password ='+ textBox2.Text +';
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da .Fill(dt);
count = Convert.ToInt32(dt.Rows.Count.ToString());
if(count == 0)
{
MessageBox.Show(用户名密码不匹配);
}
其他
$
this.Hide();
mdi_user mu = new mdi_user();
mu .Show();
}
}
private void Login_Load(object sender,EventArgs e)
{
if(con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
}
}
I'm having problem in executing non query sql statement. Bear in mind, I'm still new to .NET framework and MySQL. Here is my code.
private void Binding() { string connStr = "datasource=localhost;port=3306;username=root;password=root;"; conn = new MySqlConnection(connStr); MySqlCommand command = conn.CreateCommand(); try { string database = schemaForm.getData; dtable = new DataTable(); bindingSource = new BindingSource(); ; conn.Open(); command.CommandText = "SELECT Metabolite_Name" + "FROM " + database + ".Metabolites WHERE"+ " MetaboliteID IN ('met1', 'met2');"; command.ExecuteNonQuery(); sqlData.SelectCommand = command; sqlData.Fill(dtable); bindingSource.DataSource = dtable; dbMetName.DataSource = dtable; dtable.Columns.Add("Metabolite Name"); dbMetName.DataSource = dtable; conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Passing value from getData form
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; namespace DynamicSimulator_v2 { public partial class SchemaName : Form { private static string data; public SchemaName() { InitializeComponent(); } private void btnCancel_Click(object sender, EventArgs e) { this.Hide(); } private void btnOK_Click(object sender, EventArgs e) { data=txtDB.Text; this.Hide(); } public string getData { set { data = txtDB.Text; } get { return data; } } } }
The error shows that my query is wrong. I pretty sure the query was right as I tested it on MySQL script. And I try to show it in datagridview by the way. dbMetName is datagridview.
Quote:command.ExecuteNonQuery();
is used for Insert/Update
You should be using ExecuteReader and load the data to DataTable and finally bind to Grid.
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(dr);
public partial class Login : Form
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Library_management_system;Integrated Security=True;User Instance=True");
int count = 0;
public Login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd=con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Library_person where usernaem='"+textBox1.Text+"' and password='"+textBox2.Text+"'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
count=Convert.ToInt32(dt.Rows.Count.ToString());
if(count==0)
{
MessageBox.Show("username password does not match");
}
else
{
this.Hide();
mdi_user mu = new mdi_user();
mu.Show();
}
}
private void Login_Load(object sender, EventArgs e)
{
if(con.State==ConnectionState.Open)
{
con.Close();
}
con.Open();
}
}
这篇关于c#.net中的执行查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!