在一个按钮中添加下面的代码,当程序到“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();
}
}

解决方案 »

  1.   

    代碼看似沒問題,你把string   sqlConnection   =   "server=local;database=pubs;uid=sa;pwd=sa"; 
    中的server 改成datasource 试试
      

  2.   

    但是在我的机器上测试,根本就无法执行。难道是SQL需要进行什么设置吗?还是在ODBC中需要进行一些相关的设置。请明白的朋友指教一下,请写的详细些。我是个C#新手,谢谢!
      

  3.   

    试试这个SqlConnection("Data Source=(local);Initial Catalog=DatabaseName;" + "Integrated Security=SSPI;")
      

  4.   

    试试这个SqlConnection("Data   Source=(local);Initial   Catalog=DatabaseName;"   +   "Integrated   Security=SSPI;")
    这个办法不行,.NET提示“不支持关键字“Data Source”
    我的QQ号是61414253。哪位朋友要是网上快的话,可以使用远程协助!本人是个.NET新手。如果数据库连接不上的话,就无法进行下面的学习。
      

  5.   

    连接字不对吧 你可以用工具箱里的数据里的SqlConnection创建个连接,按照提示完成连接,之后能在设计器里发现连接字
      

  6.   

    请朋友们尽量提供使用SQL语句联接数据库,谢谢。本人不胜感激!
      

  7.   

    把 cn.Open(); 
    放在SqlCommand   cmd=new   SqlCommand(); 上一行试试呢?
      

  8.   

    把   cn.Open();   
    放在SqlCommand       cmd=new       SqlCommand();   上一行试试呢?不行呀,只要是一执行到 cn.Open(); 这句话,系统就出现异常。我安了卡巴6.0版,和卡巴斯基有关系吗?我把卡巴关了,语句也无法执行!
      

  9.   

    在vs2005上通过调试看懂这段代码就行了
    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();
    }
    }
      

  10.   

    string   sqlConnection   改成string sqlstr,下面的引用记得一起改