English 中文(简体)
无法利用与C#的分化查询显示数据
原标题:unable to display data using parametrized query with c#

我正试图利用分化查询显示数据。

try
{
 SqlConnection xconn = new SqlConnection();
 xconn.ConnectionString = @" Data Source=servername; Database=master; Trusted_Connection=yes ";
 xconn.Open();
 SqlCommand ycmd = new SqlCommand ("select * from tablename where column1 = @name", xconn);
 ycmd.Parameters.Add("@name", dropdownlist.SelectedValue);
 SqlDataAdapter da = new SqlDataAdapter(s,xconn);
 SqlCommandBuilder cmdbuilder  = new SqlCommandBuilder(da);

 DataTable dt = new DataTable();
 da.Fill(dt);
 gridview.DataSource = dt;
 gridview.DataBind();
}
catch(Exception ex)
{
  label.Text = ex.Message + "
" + ex.StackTrace;
 }

我如何工作?

最佳回答

为此:

try 
{ 
 SqlConnection xconn = new SqlConnection(); 
 xconn.ConnectionString = @" Data Source=servername; Database=master; Trusted_Connection=yes";
 SqlCommand ycmd = new SqlCommand ("select * from tablename where column1 = @name", xconn); 
 ycmd.Parameters.Add("@name", dropdownlist.SelectedValue); 
 SqlDataAdapter da = new SqlDataAdapter(ycmd);

 DataTable dt = new DataTable(); 
 da.Fill(dt); 
 gridview.DataSource = dt; 
 gridview.DataBind(); 
} 
catch(Exception ex) 
{ 
  label.Text = ex.Message + "
" + ex.StackTrace; 
}

当你使用<代码>SqlConnection.Open(>时,无需打上<条码>。 在这种方法中,它打开了连接,并在完成后处置/披露。 (这个问题,仅是FYI)

您创建<代码>SqlDataAdapter的方式是问题。 你用<条码>SqlCommand作为构造者,仅以指挥文本制作。 因此,你在<代码>SqlCommand类别中规定的参数上打了笔通行证。

让我知道这是否可行。 如果这项工作不成功,则试图在SSMS中手工操作这一询问,以确保其实际回报所设定的结果。 还确保<代码> ListControl.S selectedValue property includes some. 是否通过偷窃和分析那里储存的东西来做到这一点。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签