在一个按钮中添加下面的代码,当程序到“cn.Open();”时,系统总是异常"未将对象引用设置到对象的实例"。代码如下:
string sqlConnection = "server=local;database=pubs;uid=sa;pwd=sa";
SqlConnection cn=new SqlConnection(sqlConnection );
SqlCommand cmd=new SqlCommand();
cmd.CommandText="Delete from authors where au_lname='Smith'";
cmd.Connection=cn;
cmd.CommandType=CommandType.Text;
try
{
cn.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if(cn.State==ConnectionState.Open)
{
cn.Close();
}
}
string sqlConnection = "server=local;database=pubs;uid=sa;pwd=sa";
SqlConnection cn=new SqlConnection(sqlConnection );
SqlCommand cmd=new SqlCommand();
cmd.CommandText="Delete from authors where au_lname='Smith'";
cmd.Connection=cn;
cmd.CommandType=CommandType.Text;
try
{
cn.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if(cn.State==ConnectionState.Open)
{
cn.Close();
}
}
解决方案 »
- 如何用C# 编写数据处理程序,请高手指教!
- 像csdn、奥运购门票网站、taobao、新浪、网易是怎样架服务器的哟?
- Winfrom ComboBox 不显示垂直滚动条,如何实现。
- VC#中如何获取子菜单在父菜单中的索引位置?
- 关于gridview中radiobutton列的焦点问题
- 关于Sql Server&StreamReader耗费资源的解决办法.
- 请问一个ListView控件选择的问题
- 兄弟们给小弟推荐一本c#进阶教程~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 怎么获取某字符在字串中的位置?
- sqlConnection中的连接字符串如何通过IP设置连接数据库?
- 关于.net中显示数据源的问题
- 广告轮播中时间转换问题
中的server 改成datasource 试试
这个办法不行,.NET提示“不支持关键字“Data Source”
我的QQ号是61414253。哪位朋友要是网上快的话,可以使用远程协助!本人是个.NET新手。如果数据库连接不上的话,就无法进行下面的学习。
放在SqlCommand cmd=new SqlCommand(); 上一行试试呢?
放在SqlCommand cmd=new SqlCommand(); 上一行试试呢?不行呀,只要是一执行到 cn.Open(); 这句话,系统就出现异常。我安了卡巴6.0版,和卡巴斯基有关系吗?我把卡巴关了,语句也无法执行!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace p17_Flight
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmflight : System.Windows.Forms.Form
{
private SqlConnection objsqlcon; //看这1====================
private SqlCommand objsqlcommand;
private string insCmd;//
private string modCmd;
private string delCmd;////看这2,删除==================
private string selCmd;
。
static void Main()
{
Application.Run(new frmflight());
}
//添加
private void btnAdd_Click(object sender, System.EventArgs e)
{
if(this.txtFlightCode.Text.Equals(""))
{
MessageBox.Show("******请输入航班号******");
}
else
{
//连库
objsqlcon=new SqlConnection("server=.;database=Flights;uid=sa;"); //看这3,==========
//sql语句
insCmd=string.Format("insert into Flight values('{0}','{1}','{2}','{3}','{4}','{5}',{6})",
this.txtFlightCode.Text,this.txtAriLine.Text,
this.txtDestination.Text,this.txtSource.Text,
this.txtDeparture.Text,this.txtarrival.Text,
Convert.ToInt32(this.cboSeats.SelectedItem.ToString()));
objsqlcommand=new SqlCommand(insCmd,objsqlcon);
try
{
objsqlcon.Open();//看这3,=================
objsqlcommand.ExecuteNonQuery();
MessageBox.Show("已成功添加记录");
this.txtFlightCode.Text="";
this.txtAriLine.Text="";
this.txtDestination.Text="";
this.txtSource.Text="";
this.txtDeparture.Text="";
this.txtarrival.Text="";
}
catch(SystemException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//关闭
objsqlcon.Close();//看这3,==============
}
}
}
//删除
private void btnDelete_Click(object sender, System.EventArgs e)
{
//连库
objsqlcon=new SqlConnection("server=.;database=Flights;uid=sa;");//看这4,=================
//sql语句//看这4,==================
delCmd=string.Format("delete from Flight where FlightCode='{0}'",this.txtFlightCode.Text); objsqlcommand=new SqlCommand(delCmd,objsqlcon);
try
{
if(this.txtFlightCode.Text.Equals(""))
{
MessageBox.Show("******请输入航班号******");
}
else
{
DialogResult result=MessageBox.Show("确定要删除当前记录吗?","确定",MessageBoxButtons.YesNo);
if(result.Equals(DialogResult.Yes))
{
objsqlcon.Open();//看这5,====================
objsqlcommand.ExecuteNonQuery();
MessageBox.Show("已删除记录"); this.txtFlightCode.Text="";
this.txtAriLine.Text="";
this.txtDestination.Text="";
this.txtSource.Text="";
this.txtDeparture.Text="";
this.txtarrival.Text="";
}
}
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally //看这6,=========================
{
objsqlcon.Close();
}
}