private void button2_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
//定义删除SQL语句
string sqlStr = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
SqlCommand sqlcmd = new SqlCommand(sqlStr, sqlcon);//实例化SqlCommand对象
if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
{
sqlcon.Open();//打开数据库连接
}
int records = Convert.ToInt32(sqlcmd.ExecuteNonQuery());//执行删除命令
if (records > 0)//判断删除是否成功
label2.Text="删除成功!";
else
label2.Text="删除失败!";
sqlcmd.Dispose();//释放SqlCommand对象资源
sqlcon.Close();//关闭数据库连接
Form1_Load(sender, e);//刷新窗体
}
private void button1_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
//定义删除SQL语句
string sqlStr1 = "insert into IlluminationInfo(Roomnumber,lampnumber,consumption,cost,lamppower)values(" + Convert.ToInt32(textBox1.Text) + "," + Convert.ToInt32(textBox2.Text) + "," + Convert.ToInt32(textBox3.Text) + "," + Convert.ToInt32(textBox4.Text) + "," + Convert.ToInt32(textBox5.Text) + ")";
SqlCommand sqlcmd1 = new SqlCommand(sqlStr1, sqlcon);//实例化SqlCommand对象
//string sqlStr2 = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
//SqlCommand sqlcmd2 = new SqlCommand(sqlStr2, sqlcon);//实例化SqlCommand对象
if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
{
sqlcon.Open();//打开数据库连接
}
int records1 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
if (records1 > 0)//判断删除是否成功
label2.Text = "添加成功!";
else
label2.Text = "添加失败!";
//if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
//{
// sqlcon.Open();//打开数据库连接
//}
//int records2 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
// if (records2 > 0)//判断删除是否成功
// label2.Text = "tj成功!";
//else
// label2.Text = "tj失败!";
sqlcmd1.Dispose();//释放SqlCommand对象资源
sqlcon.Close();//关闭数据库连接
Form1_Load(sender, e);//刷新窗体
}
这是我的程序片段
我在界面上设了一个button2删除按钮 和button添加按钮 为什么不能同时执行呢?
请各位大侠帮帮我
{
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
//定义删除SQL语句
string sqlStr = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
SqlCommand sqlcmd = new SqlCommand(sqlStr, sqlcon);//实例化SqlCommand对象
if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
{
sqlcon.Open();//打开数据库连接
}
int records = Convert.ToInt32(sqlcmd.ExecuteNonQuery());//执行删除命令
if (records > 0)//判断删除是否成功
label2.Text="删除成功!";
else
label2.Text="删除失败!";
sqlcmd.Dispose();//释放SqlCommand对象资源
sqlcon.Close();//关闭数据库连接
Form1_Load(sender, e);//刷新窗体
}
private void button1_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
//定义删除SQL语句
string sqlStr1 = "insert into IlluminationInfo(Roomnumber,lampnumber,consumption,cost,lamppower)values(" + Convert.ToInt32(textBox1.Text) + "," + Convert.ToInt32(textBox2.Text) + "," + Convert.ToInt32(textBox3.Text) + "," + Convert.ToInt32(textBox4.Text) + "," + Convert.ToInt32(textBox5.Text) + ")";
SqlCommand sqlcmd1 = new SqlCommand(sqlStr1, sqlcon);//实例化SqlCommand对象
//string sqlStr2 = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
//SqlCommand sqlcmd2 = new SqlCommand(sqlStr2, sqlcon);//实例化SqlCommand对象
if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
{
sqlcon.Open();//打开数据库连接
}
int records1 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
if (records1 > 0)//判断删除是否成功
label2.Text = "添加成功!";
else
label2.Text = "添加失败!";
//if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
//{
// sqlcon.Open();//打开数据库连接
//}
//int records2 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
// if (records2 > 0)//判断删除是否成功
// label2.Text = "tj成功!";
//else
// label2.Text = "tj失败!";
sqlcmd1.Dispose();//释放SqlCommand对象资源
sqlcon.Close();//关闭数据库连接
Form1_Load(sender, e);//刷新窗体
}
这是我的程序片段
我在界面上设了一个button2删除按钮 和button添加按钮 为什么不能同时执行呢?
请各位大侠帮帮我
解决方案 »
- 程序用存储过程向数据库添加数据,怎么样反回具体错误信息
- 谁能帮帮小妹,获取gridview 里textbox的值时“未将对象引用设置到对象的实例”
- datagrid行显示控制(根据行中的某一列的值,去定义此行是否显示)
- C#中,有没有像C++一样的标准库可以使用?比如vector和Stack之类的东西,目前我就知道有ArrayList
- 怎么用C#在B/S架构下上传图片
- 为什么我的vs2005中没有DataTable
- 如何实现选中DATAGRID的CHECKBOX后激发事件:将当前选中行的数据写入数据库。
- 请高手指点
- ASP.NET中如何实现编辑DataGrid的Head
- 如何获得当鼠标单击windowsapp标题栏后就可以实现winform移动的效果。。如果在别的控件上实现同样的功能需要将鼠标定义为哪种动作事件
- LISTBOX怎么显示当前最新数据
- 为什么有的书介绍 winform的登录信息要保存到ini文件
现在的新问题是我为了验证它是不是可以在button2下是否能执行 在Form窗体上删除了button1 结果还是不行
最后我把button1又从toolbox里面放回Form1,改好名字以后 运行成功,但是没办法连接库,就是往数据库里面添加 删除不了啦
请各位大侠帮忙
谢谢:)
{
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
//定义删除SQL语句
string sqlStr = "delete from IlluminationInfo where Roomnumber=" + Convert.ToInt32(comboBox1.Text) + "";
if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
{
sqlcon.Open();//打开数据库连接
}
SqlCommand sqlcmd = new SqlCommand(sqlStr, sqlcon);//实例化SqlCommand对象
int records = Convert.ToInt32(sqlcmd.ExecuteNonQuery());//执行删除命令
if (records > 0)//判断删除是否成功
label2.Text="删除成功!";
else
label2.Text="删除失败!";
sqlcmd.Dispose();//释放SqlCommand对象资源
sqlcon.Close();//关闭数据库连接
Form1_Load(sender, e);//刷新窗体
}
private void button1_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
//定义删除SQL语句
string sqlStr1 = "insert into IlluminationInfo(Roomnumber,lampnumber,consumption,cost,lamppower)values(" + Convert.ToInt32(textBox1.Text) + "," + Convert.ToInt32(textBox2.Text) + "," + Convert.ToInt32(textBox3.Text) + "," + Convert.ToInt32(textBox4.Text) + "," + Convert.ToInt32(textBox5.Text) + ")";
if (sqlcon.State == ConnectionState.Closed)//判断连接是否关闭
{
sqlcon.Open();//打开数据库连接
} SqlCommand sqlcmd1 = new SqlCommand(sqlStr1, sqlcon);//实例化SqlCommand对象
int records1 = Convert.ToInt32(sqlcmd1.ExecuteNonQuery());//执行删除命令
if (records1 > 0)//判断删除是否成功
label2.Text = "添加成功!";
else
label2.Text = "添加失败!";
sqlcmd1.Dispose();//释放SqlCommand对象资源
sqlcon.Close();//关闭数据库连接
Form1_Load(sender, e);//刷新窗体
}
我想问下 你每次运行的时候第一次点添加时成功吗 是不是删除不成功? 那你第一次运行时先点删除看是否成功 再点添加看是否也成功
还有你把你的SQL语句去数据库中运行一次 看有没有问题 能不能得到你要的结果