程序运行连接数据库时遇到问题了,无法连接
程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Ex09_13 { public partial class Form1 : Form { DataTable dt=null; TextBox[] txtBox; SqlConnection con = new SqlConnection("server=.;pwd=;uid=sa;database=db_09"); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { showList(); } private void showList() { listView1.View = View.Details;//视图 listView1.GridLines = true;//网格线 using (SqlDataAdapter da = new SqlDataAdapter("select * from 帐单", con)) { //生成结果集 dt = new DataTable(); da.Fill(dt); ColumnHeader ch; for (int i = 0; i < dt.Columns.Count; i++)//列 { ch = new ColumnHeader(); ch.Text = dt.Columns[i].ColumnName.ToString(); ch.Name = dt.Columns[i].ColumnName.ToString(); ch.Width = 72; this.listView1.Columns.Add(ch); } //建立结构 Method(dt); } } private void tbADD_Click(object sender, EventArgs e) { if (dt != null) { DataRow row; txtBox = new TextBox[6]; txtBox[0] = this.textBox1; txtBox[1] = this.textBox2; txtBox[2] = this.textBox3; txtBox[3] = this.textBox4; txtBox[4] = this.textBox5; txtBox[5] = this.textBox6; row = dt.NewRow(); for (int i = 0; i < dt.Columns.Count; i++) { row[dt.Columns[i].ToString()] = this.txtBox[i].Text.ToString(); } dt.Rows.Add(row); Method(dt); } } private void Method(DataTable dt) { listView1.Items.Clear(); ListViewItem listItem = null; for (int j = 0; j < dt.Rows.Count; j++) { listItem = new ListViewItem(dt.Rows[j][0].ToString()); for (int k = 1; k < dt.Columns.Count; k++) { listItem.SubItems.Add(dt.Rows[j][k].ToString()); } listView1.Items.Add(listItem); } } private void tbSave_Click(object sender, EventArgs e) { using (SqlDataAdapter da = new SqlDataAdapter()) { SqlCommand command = new SqlCommand("INSERT INTO 帐单 " + "VALUES (@员工姓名, @基本工资,@奖金,@扣款,@午餐,@实际工资)", con); // Add the parameters for the InsertCommand. command.Parameters.Add("@员工姓名", SqlDbType.VarChar, 10, "员工姓名"); command.Parameters.Add("@基本工资", SqlDbType.VarChar, 10, "基本工资"); command.Parameters.Add("@奖金", SqlDbType.VarChar, 10, "奖金"); command.Parameters.Add("@扣款", SqlDbType.VarChar, 10, "扣款"); command.Parameters.Add("@午餐", SqlDbType.VarChar, 10, "午餐"); command.Parameters.Add("@实际工资", SqlDbType.VarChar, 10, "实际工资"); da.InsertCommand = command; da.Update(dt); MessageBox.Show("以成功能将信息解析回数据库"); } } } }我从书上找的例子,但是调试出错
da.Fill(dt);就是在这句上。应该实在数据库那的问题,我在数据库里建的sa用户名但密码无法设置为空, 朋友跟我说要重装下SQL安装的时候把名字改成sa,请问大家怎么办?