|
-
May 12th, 2010, 02:20 AM
#1
Parameterized query with Select Command
string connectionString = "workstation id=AALOK;packet size=4096;user id=sa;data source=AALOK;persist security info=False;initial catalog=aks";
conn = new SqlConnection(connectionString);
string commandString = "SELECT QuesID, QuesDet, Quiz_Name FROM ak";
dataAdapter = new SqlDataAdapter(commandString, conn);
ds = new DataSet();
dataAdapter.Fill(ds,"Ak");
dataTable = ds.Tables["Ak"];
I have created an application using C# 2003 with SQL Server 2000.
The above code is working fine, but my requirement is to add "where Quiz_Name=@Quiz_Name" into the commandString and to set @Quiz_Name="Some string".
Can anyone help me to write the correct code?
-
May 12th, 2010, 06:48 AM
#2
Re: Parameterized query with Select Command
Hi,
Try something like this:
SqlCommand cmd = new SqlCommand("SELECT QuesID, QuesDet, Quiz_Name FROM ak WHERE Quiz_Name = @Quiz_Name", conn);
cmd.Parameters.AddWithValue("Quiz_Name", "Some string");
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
Hope this helps,
Alan
www.rapidqualitysystems.com - Taking automated documentation to new heights
-
May 13th, 2010, 12:46 AM
#3
Re: Parameterized query with Select Command
Thanks dear for your reply.
I have tried the code given by you and getting error:- "System.Data.SqlClient.SqlParameterCollection' does not contain a definition for 'AddWithValue'".
Aalok
-
May 13th, 2010, 04:51 AM
#4
Re: Parameterized query with Select Command
Hi Aalok,
I don't know what version of .NET you are running, but I can only assume that the AddWithValue method is not available in your version.
AddWithValue is not the only way to add a parameter. May I direct you towards the SqlCommand documentation: http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx
There are also some examples that you can try on this page: http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx
I hope this helps.
Alan
www.rapidqualitysystems.com - Taking automated documentation to new heights
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|