请人帮忙纠错
在数据库中删除一个列时抛出异常,望哥哥姐姐们帮忙看一下。#region Using Region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
#endregion
namespace DeletingData
{
class Program
{
static void Main(string[] args)
{
try
{
SqlConnection con = new SqlConnection(@"Server=(local);Integrated Security=True;Database=图书馆管理系统");
SqlDataAdapter adpt = new SqlDataAdapter("select * from administrator",con);
SqlCommandBuilder cmdbldr = new SqlCommandBuilder(adpt);
DataSet dtst = new DataSet();
adpt.Fill(dtst,"table");
Console.WriteLine("Rows before change is "+dtst.Tables["table"].Rows.Count);
//Create a key
DataColumn[] keys = new DataColumn[1];
keys[0] = dtst.Tables["table"].Columns["UserName"];
dtst.Tables["talbe"].PrimaryKey = keys; //运行到这一句时出错,异常提示:未将对象引用设置到对象的实例
//Searching for row
DataRow fRow = dtst.Tables["talbe"].Rows.Find("54456");
if (fRow != null)
{
Console.WriteLine("54456 is found.");
Console.WriteLine("Removing now...");
fRow.Delete();
adpt.Update(dtst,"table");
}
Console.WriteLine("Rows after chang is " + dtst.Tables["talbe"].Rows.Count);
con.Close();
Console.WriteLine("Press any key to exit...");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.ReadKey();
}
}
}
}
[[it] 本帖最后由 mona 于 2008-12-13 08:47 编辑 [/it]]