照书上把程序抄下来竟有错!!!
using System;
using System.Data;
using System.Data.SqlClient;class MyClass
{ public static void Main()
{
//数据库连接
SqlConnection conPubs = new SqlConnection ("server=localhost;uid=sa;pwd=;database=pubs");
conPubs.Open();
SqlDataAdapter dbAdapter = new SqlDataAdapter("select au_id,au_lname,au_fname,contract from authors",conPubs);
SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(dbAdapter);
DataSet dbset = new DataSet();
dbAdapter.Fill(dbset,"authors");
Console.WriteLine("修改前的记录数:{0}",dbset.Tables["authors"].Rows.Count); //增加行
DataRow newRow = dbset.Tables["authors"].NewRow();
newRow["au_id"] = "1111-1111-1111";
newRow["au_lname"] = "blek";
newRow["au_fname"] = "Snow";
newRow["contract"] = 1;
dbset.Tables["authors"].Rows.Add(newRow);
//添加到数据库中
try
{
dbAdapter.Update(dbset,"authors");
}
catch(Exception e)
{
Console.WriteLine("Exception occur!");
Console.WriteLine("Exception:{0}",e);
}
Console.WriteLine("修改后的记录数:{0}",dbset.Tables["authros"].Rows.Count);
conPubs.Close();
}
}现在提示有三处错误!!!
1.Console.WriteLine("修改前的记录数:{0}",dbset.Tables["authors"].Rows.Count);
2.Console.WriteLine("修改后的记录数:{0}",dbset.Tables["authros"].Rows.Count);
3.dbAdapter.Update(dbset,"authors");
还有:SqlCommandBuilder的作用是什么啊?不知道前两句为什么错?第三个问题可能是自己把这些对象没搞清楚吧?
帮帮吧,谢谢了!!!
using System;
using System.Data;
using System.Data.SqlClient;class MyClass
{ public static void Main()
{
//数据库连接
SqlConnection conPubs = new SqlConnection ("server=localhost;uid=sa;pwd=;database=pubs");
conPubs.Open();
SqlDataAdapter dbAdapter = new SqlDataAdapter("select au_id,au_lname,au_fname,contract from authors",conPubs);
SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(dbAdapter);
DataSet dbset = new DataSet();
dbAdapter.Fill(dbset,"authors");
Console.WriteLine("修改前的记录数:{0}",dbset.Tables["authors"].Rows.Count); //增加行
DataRow newRow = dbset.Tables["authors"].NewRow();
newRow["au_id"] = "1111-1111-1111";
newRow["au_lname"] = "blek";
newRow["au_fname"] = "Snow";
newRow["contract"] = 1;
dbset.Tables["authors"].Rows.Add(newRow);
//添加到数据库中
try
{
dbAdapter.Update(dbset,"authors");
}
catch(Exception e)
{
Console.WriteLine("Exception occur!");
Console.WriteLine("Exception:{0}",e);
}
Console.WriteLine("修改后的记录数:{0}",dbset.Tables["authros"].Rows.Count);
conPubs.Close();
}
}现在提示有三处错误!!!
1.Console.WriteLine("修改前的记录数:{0}",dbset.Tables["authors"].Rows.Count);
2.Console.WriteLine("修改后的记录数:{0}",dbset.Tables["authros"].Rows.Count);
3.dbAdapter.Update(dbset,"authors");
还有:SqlCommandBuilder的作用是什么啊?不知道前两句为什么错?第三个问题可能是自己把这些对象没搞清楚吧?
帮帮吧,谢谢了!!!
解决方案 »
- 什么时候才可以用data = ConvertUnicodeToGBK(textBox_Send.Text);
- 创建触发器后就插入不了数据???
- C#中的继承
- 关于.NET连接ACCESS数据库的问题.在线等!~~~
- 如何把XML文件读成一个完整DataSet通过WebService取出DataSet里的某几个Table然后入库,在线等!(100分)
- 关于comboBox的问题(新)
- ?我编了一个使用udp协议的socket服务端程序,无法接收到数据,大家看看有问题吗?
- 那里有好用的web打印控件?
- 有关datagrid的问题,请问高手?
- c++ builder 封装了一个dll,c#调用报错,求助!!
- C#From 编程---MDI子窗体最大化的时候出现的怪问题??
- 如何在多个web service以及和本地中共享一套接口规范
newRow["au_lname"] = "blek";
newRow["au_fname"] = "Snow";这里有有一个字段长度>数据库里的定义的长度了。
你说的那几个都没错。
这是运行到这一行出现的错误:
未处理的“System.NullReferenceException”类型的异常出现在 ConsoleApplication1.exe 中。3.dbAdapter.Update(dbset,"authors");
运行这一行抛出的异常为:
Exception occur!
Exception:System.Data.SqlClient.SqlException: 将截断字符串或二进制数据。
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
at MyClass.Main()
newRow["au_id"] = "998-72-3568";
其输出结果为:
修改前的记录数:23
未处理的异常: System.NullReferenceException: 未将对象引用设置到对象的实例。
at MyClass.Main()
但我把
2.Console.WriteLine("修改后的记录数:{0}",dbset.Tables["authros"].Rows.Count);这一句加注释后,输出为:
修改前的记录数:23
又没有异常抛出,是什么原因啊?