sql 和C#的连接问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 10:54:14
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ERP.Properties.Settings.LongKaiERPConnectionString"].ConnectionString);

conn.Open();

SqlCommand command = new SqlCommand(String.Format("insert into supplyCompany values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", this.textBox1.Text, this.textBox2.Text,this.dateTimePicker1.Text,this.textBox5.Text,this.textBox3.Text,this.textBox4.Text,this.comboBox1.Text,this.checkBox1.Text,this.textBox6.Text,this.textBox7.Text), conn);
command.ExecuteNonQuery();

command = new SqlCommand("select * from supplyCompany", conn);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())

建议把插入语句改掉,在command之前声明strSql="insert into supplyCompany values …… ",不要使用String.Format试试。

trycatch
看看错误信息

SqlConnection conn = new SqlConnection("server = localhost;integrated seurity = true; database = 你要连的数据库名字")试试这个连

所以很可能是连接字符串出了问题
是不是只连接到服务器
没连接到数据库呢?

你可以这样连接,试试..
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=数据库名");