作者在 2010-11-23 13:18:37 发布以下内容
刚学到数据库编程,书上的文字、代码一大堆,是人没耐心,恼火!
一个简单的c#与数据库的连接,不过短短6行代码,我就弄了好一会儿,粗心!我把“Data Source”写成了“DataSource”,结果连不上,后来终于发现,
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 zsg
{
public partial class Form1 : Form
{
private SqlConnection dbConnection;
public Form1()
{
InitializeComponent();
}
//连接数据库
private void button1_Click(object sender, EventArgs e)
{
string strConn;
try
{
dbConnection = new SqlConnection();
strConn = "Data Source=localhost;";
strConn += "Initial Catalog=lunwen;";
strConn += "Integrated Security=True";
dbConnection.ConnectionString = strConn;
label4.Text = "连接成功";
}
catch (Exception e3)
{
MessageBox.Show(e3.Message);
}
}
}
}
要记住教训,细工出好活。要有耐心。付出总有回报。
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace zsg
{
public partial class Form1 : Form
{
private SqlConnection dbConnection;
public Form1()
{
InitializeComponent();
}
//连接数据库
private void button1_Click(object sender, EventArgs e)
{
string strConn;
try
{
dbConnection = new SqlConnection();
strConn = "Data Source=localhost;";
strConn += "Initial Catalog=lunwen;";
strConn += "Integrated Security=True";
dbConnection.ConnectionString = strConn;
label4.Text = "连接成功";
}
catch (Exception e3)
{
MessageBox.Show(e3.Message);
}
}
}
}