ExecuteNonQuery:Connection属性尚未初始化
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection("server=Localhost;database=MyData;uid=sa;pwd=123");//申明连接
conn.Open();//打开连接
if (conn.State == ConnectionState.Open)//判断连接是否打开
{
MessageBox.Show("数据库连接打开");
}
else { MessageBox.Show("数据库连接关闭"); }
}
catch(Exception ex)
{
MessageBox.Show("异常" + ex.Message);
}
}
private void bt_insert_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection("server=Localhost;database=MyData;uid=sa;pwd=123");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into Stu values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "'), conn");
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("插入成功");
button6_Click(sender, e);
}
}
catch(Exception ex)
{
MessageBox.Show("异常" + ex.Message);
}
}
private void button6_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=Localhost;database=MyData;uid=sa;pwd=123");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Stu",conn);
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
adp.SelectCommand = cmd;
adp.Fill(ds);
}
}
}
运行后提示ExecuteNonQuery:Connection属性尚未初始化,大家帮帮忙呀,急 展开
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection("server=Localhost;database=MyData;uid=sa;pwd=123");//申明连接
conn.Open();//打开连接
if (conn.State == ConnectionState.Open)//判断连接是否打开
{
MessageBox.Show("数据库连接打开");
}
else { MessageBox.Show("数据库连接关闭"); }
}
catch(Exception ex)
{
MessageBox.Show("异常" + ex.Message);
}
}
private void bt_insert_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection("server=Localhost;database=MyData;uid=sa;pwd=123");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into Stu values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "'), conn");
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("插入成功");
button6_Click(sender, e);
}
}
catch(Exception ex)
{
MessageBox.Show("异常" + ex.Message);
}
}
private void button6_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=Localhost;database=MyData;uid=sa;pwd=123");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Stu",conn);
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
adp.SelectCommand = cmd;
adp.Fill(ds);
}
}
}
运行后提示ExecuteNonQuery:Connection属性尚未初始化,大家帮帮忙呀,急 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询