填充数据集时出现问题
这是我的代码文件的一小部分。每次我的调试器到达“NewDA.Fill(NewDS);”行时在运行时它会跳转到 catch 处。我确信 daynumber 变量会获取数据库中存在的值,并且我已经在数据库的代码文件之外尝试了查询,并且工作正常。我还在代码的更多部分使用了连接字符串“db”,并取得了成功的结果。
string QueryNew = "SELECT activityname AS [Name], activitycategorynumber AS [Category] " +
"FROM ACTIVITY WHERE daynumber = @daynumber";
SqlCommand NewCmd = new SqlCommand(QueryNew, db);
NewCmd.Parameters.Add("@daynumber", SqlDbType.Int).Value = daynumber;
SqlDataAdapter NewDA = new SqlDataAdapter(NewCmd);
DataSet NewDS = new DataSet();
NewDA.Fill(NewDS);
This is a small portion of my code file. Each time my debugger reaches the line 'NewDA.Fill(NewDS);' at runtime it jumps to the catch. I'm positive the daynumber variable gets a value that's present in the database and I've tried the query outside of the codefile on my database and it works fine. I'm also using the connectionstring 'db' on more parts of the code with successful results.
string QueryNew = "SELECT activityname AS [Name], activitycategorynumber AS [Category] " +
"FROM ACTIVITY WHERE daynumber = @daynumber";
SqlCommand NewCmd = new SqlCommand(QueryNew, db);
NewCmd.Parameters.Add("@daynumber", SqlDbType.Int).Value = daynumber;
SqlDataAdapter NewDA = new SqlDataAdapter(NewCmd);
DataSet NewDS = new DataSet();
NewDA.Fill(NewDS);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否还验证了数据库中的 daynumber 是一个 int ?抛出的异常应该为您提供有关错误的更多详细信息。
Have you also verified that daynumber an int in the database? The exception thrown should give you more details about the error.
您尝试过的地方
NewDA.Fill(NewDS);
相反,请尝试
NewDA.Fill(NewDS,"");
如果您的表名称是
ACTIVITY
,则尝试NewDA.Fill(NewDS ,“活动”);
Where you tried
NewDA.Fill(NewDS);
Instead of it, try
NewDA.Fill(NewDS,"<table_name>");
If your table name is
ACTIVITY
, then tryNewDA.Fill(NewDS,"ACTIVITY");