datagridview数据显示问题

benbenkui 2008-02-25 08:29:45
SqlConnection con = DB.createConn();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "train_no";
cmd.Parameters.Add( "@tr_no",SqlDbType.VarChar,8).Value=train_no;//train_no已经赋值
dataGridView1.DataSource = bindingSource1;
GetDate(cmd.CommandText);
报错,存储过程参数没有提供。
public void GetDate(string sqlCmd)
{
SqlConnection conn=DB.createConn();
SqlDataAdapter sda=new SqlDataAdapter (sqlCmd,conn);
SqlCommandBuilder cmdbuilder=new SqlCommandBuilder (sda);
DataTable tb=new DataTable ();
//获取或设置用于比较表中字符串的区域设置信息
tb.Locale=System.Globalization.CultureInfo.InvariantCulture;
sda.Fill(tb);
bindingSource1.DataSource=tb;
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
当sqlcmd是SQL查询语句时,成功运行。
当是存储过程时,报错:“过程或函数 'train_no' 需要参数 '@tr_no',但未提供该参数”。



...全文
176 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
SqlCommandBuilder cmdbuilder=new SqlCommandBuilder (sda);
把这句去掉,这句是对单一表自动生成update,delete,insert语句的。
你用的存储过程不需要这句话。
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
衷心感谢“lye2000000_super 我自横刀香甜笑”,不厌其烦的帮我这个菜鸟
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
改了一下,却还是老问题。
调用getdate(sqlcomand cmd)时,
CommandText = "train_no_check"
CommandType = StoredProcedure
当执行SqlCommandBuilder cmdbuilder=new SqlCommandBuilder (sda);
CommandText = "train_no_check"
而CommandType = text
我在前面甚至都加了:cmd.CommandType=CommandType.StoredProcedure;
为什么CommandType值会自动变为text呢?


  • 打赏
  • 举报
回复
public void GetDate(SqlCommand sqlCmd)
改成这样就可以了
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
是不是应该在GetDate()中指定CommandType=CommandType.StoredProcedure?
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
发现了错误信息,在sda中selectCommand.commandType=Text
  • 打赏
  • 举报
回复
public void GetDate(string sqlCmd)
的sqlCmd,调用这个方法的时候,传进去的值是什么?不是错误信息。
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
未处理 System.Data.SqlClient.SqlException
Message="过程或函数 'train_no_check' 需要参数 '@tr_no',但未提供该参数。"
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=16
LineNumber=0
Number=201
Procedure="train_no"
Server="ZHANGKUI\\SQLEXPRESS"
State=4
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.ConsumeMetaData()
在 System.Data.SqlClient.SqlDataReader.get_MetaData()
在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
在 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
在 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
在 System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
在 System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
在 trTimeQuery.Form1.GetDate(String sqlCmd) 位置 C:\Documents and Settings\benben\My Documents\Visual Studio 2005\Projects\trTimeQuery\trTimeQuery\Form1.cs:行号 32
在 trTimeQuery.Form1.btnTCheck_Click(Object sender, EventArgs e) 位置 C:\Documents and Settings\benben\My Documents\Visual Studio 2005\Projects\trTimeQuery\trTimeQuery\Form1.cs:行号 78
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 trTimeQuery.Program.Main() 位置 C:\Documents and Settings\benben\My Documents\Visual Studio 2005\Projects\trTimeQuery\trTimeQuery\Program.cs:行号 17
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
  • 打赏
  • 举报
回复
你的sqlCmd这个参数传进去的是什么?跟踪调试以下,贴出来是什么值?
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
不是空值
conan304 2008-02-25
  • 打赏
  • 举报
回复
看看texTrNo这个TextBox是不是空值??
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
train_no已经赋值了,忘了贴出来。string train_no=txtTrNo.Text.Trim();
conan304 2008-02-25
  • 打赏
  • 举报
回复
            SqlConnection con = DB.createConn();
SqlCommand cmd = con.CreateCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "train_no";
string train_no = "";//在这里赋值;
cmd.Parameters.Add("@tr_no", SqlDbType.VarChar, 8).Value = train_no;//train_no已经赋值
dataGridView1.DataSource = bindingSource1;
GetDate(cmd.CommandText);
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
GataDate()是不是有问题啊?
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
还是在 sda.Fill(tb); 报错(没提供参数值)
csrwgs 2008-02-25
  • 打赏
  • 举报
回复
你按照
SqlParameter para = new SqlParameter("@tr_no", SqlDbType.VarChar);
para.Value = train_no;
cmd.Parameters.Add(para);
写应该是可以的.
benbenkui 2008-02-25
  • 打赏
  • 举报
回复
自己先顶下,大家帮看下

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧