winform编码连接sql,调试没有反应
form里拖放一个DataGridView和一个BindingSource,编译没有错误
代码如下:
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 System.Data.SqlClient;
namespace 快餐店win代码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string conn_string = "Data Source=yh-20120602uzvz\\SQLNEW;Initial Catalog=FastFood;Integrated Security=True";//连接字符串
string sql = "Select CustomerID,userName,Password,Email form Customer";//查询select语句
SqlConnection conn = new SqlConnection(conn_string);//创建连接对象
SqlCommand cmd = new SqlCommand();//创建执行SQL命名形象
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
SqlDataReader customer1 = cmd.ExecuteReader(); //执行查询的select语句,并产生结果集
bindingSource1.DataSource = customer1;//把结果与bindingsource控件相结合并绑定
dataGridView1.DataSource = bindingSource1;//把dataGridView控件与bindingSource相结合并绑定
}
}
}
[解决办法]
至少我看到的sql语句是错误的,应该是from ,而不是form
Select CustomerID,userName,Password,Email from Customer
[解决办法]
private void Form1_Load(object sender, EventArgs e)
是自己敲的,不是自动生成的吧
[解决办法]
你能把数据库连接关掉么?
conn.Close();
[解决办法]