using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
SqlConnection sc = new SqlConnection();//重载,命名数据。
try//单击button1_Click之后发生的事件
{
sc.ConnectionString = "Data Source=.; Initial Catalog=CJXYMis;User ID=sa;Pwd=1";
sc.Open();
MessageBox.Show("数据库连接成功!");
SqlCommand scm = new SqlCommand();//定义一个新的数据方法。 scm.Connection = sc;//
string x = "select * from DM_GB_XKFLYDM where DM like '" + this.textBox1.Text + "'";
//在DM_GB_XKFLYDM的*(所有列)在textBox1控件的Text中按照DM列查找。
//查找项
scm.CommandText = x;
//查询命令
DataTable table = new DataTable();
//新建虚拟表
SqlDataAdapter sda = new SqlDataAdapter();//
sda.SelectCommand = scm;//
sda.Fill(table);
//
this.dataGridView1.DataSource = table;//
sc.Close();//
}
catch
{
MessageBox.Show("该数据不存在!");
}
} //关闭窗口
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
已经知道了,是数据库sa登陆失败,但是我数据是试用windows默认密码,电脑本身没有设定密码的,你知道大概错在那儿吗?
追答好就是连接数据库不成功嘛,代码写的没有问题!你先搞明白怎么登录数据库,最好是设置一个独立的登录用户和密码,不要与windows相同。
本回答被提问者采纳