读书人

求救!将数据库查询结果放入DataSet时

发布时间: 2011-12-10 22:22:01 作者: rapoo

求救!!!将数据库查询结果放入DataSet时发生 varchar型转化成int的错误
我将查询的结果放入DataSet结果集时发生类型转换错误,但又不知错误发生在哪,还请各位前辈多多指教小弟

下面是错的代码

public partial class p2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
initData();

}
private void initData()
{
string strID = Request.QueryString["id"];

Response.Write(strID);


SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = "Type System Version=SQL Server 2000;persist Security Info=False;" +
"Integrated Security=true;database=pubs;server=(local)";
myCon.Open();

SqlCommand selectCMD = new SqlCommand("Select * from authors "+
"where au_id="+strID, myCon);



SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selectCMD;

DataSet ds = new DataSet();
da.Fill(ds); //异常抛出点,ErrorMessage="将 varchar 值 '172-32-1176' 转换为数据类型为
//int 的列时发生语法错误。"

this.Label1.Text = "ID: "+ds.Tables[0].Columns["au_id"];

this.Label2.Text = "Name: ";
this.Label1.Text = "Phone: ";
this.Label1.Text = "Address: ";
myCon.Close();

}
}

下面是debug的异常详细信息,小弟是新手,不明白哪里发生了差错


用户代码未处理 System.Data.SqlClient.SqlException
Message="将 varchar 值 '172-32-1176' 转换为数据类型为 int 的列时发生语法错误。"
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=245
Procedure=""
Server="(local)"
State=1
StackTrace:
在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
在 System.Data.SqlClient.SqlDataReader.HasMoreRows()
在 System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
在 System.Data.SqlClient.SqlDataReader.Read()
在 System.Data.ProviderBase.DataReaderContainer.Read()
在 System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
在 System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
在 System.Data.Common.DataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
在 p2.initData() 位置 d:\我的代码\The code of C#\DataBind\p2.aspx.cs:行号 41
在 p2.Page_Load(Object sender, EventArgs e) 位置 d:\我的代码\The code of C#\DataBind\p2.aspx.cs:行号 17


在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
在 System.Web.UI.Control.OnLoad(EventArgs e)
在 System.Web.UI.Control.LoadRecursive()
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


[解决办法]
如果你的au_id字段的类型为非数值型,那么试试

C# code
 SqlCommand selectCMD = new SqlCommand("Select * from authors where au_id='"+strID+"'", myCon); 

读书人网 >asp.net

热点推荐