HighTechTalks DotNet Forums  

Re: SQL in C#: How to - 2 questions

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss Re: SQL in C#: How to - 2 questions in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jim Underwood
 
Posts: n/a

Default Re: SQL in C#: How to - 2 questions - 11-02-2005 , 04:59 PM






First, I use vb.net, but the two are pretty similar so this should work
anyway...

1. some suggestions...
First, put your SQL into a string variable, then pass the variable as the
command text. This makes no difference technically, but makes the code
easier to read and simplifies debugging.
Second, output the SQL String before you execute it. If you have trouble
you can cut and paste the string itself to execute it manually in your
database tool. Then you can fix the SQL outside of VisualStudio and
seperate SQL errors from C# errors.

Soem problems I see...
the field name "IP Address" is invalid becuase it contains a space. You may
have the wrong field name, or you may need to place [] around the field name
in your SQL.
you are usign your variables in your SQL without concatenating them. I think
this should through an error when compiling. Add in the + for
concatenation.

Also, although concatenation works for your example, you want to use
parameters in place of concatenation or you run into security issues. This
is nto really a problem for academic programs, but worht looking into once
you get the hang of basic queries.

string mySQL ;
mySQL = "select * from Customer where [IP Address] = '" + IP + "' OR City =
'" + city + "'";
oleDbDataAdapter1.SelectCommand.CommandText = mySQL

2. To check if the dataset is empty try using is nothing (this works in VB,
not certain about C#.
if (dataSet1 is nothing)


"tiger" <aaa0715 (AT) omega (DOT) uta.edu> wrote

Quote:
Hi,

1.) I have the following queries in C# code but when I try to run it I get
error message saying expecting (;,;, in my query

string theReply = "";
string theReply1 = "";
theReply = reader.ReadString();
theReply1 = reader.ReadString();

IP = theReply;
city = theReply1;

oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM " +
"Customer WHERE IP Address = '"IP"' OR City = '"city"'";

dataSet1.Clear();

oleDbDataAdapter1.Fill(dataSet1, "Customer");
dataGrid1.SetDataBinding(dataSet1,"Customer");

if (dataSet1 == NULL)
{
textBox.Text += "Hey";
}

error message below...

: expected
:expected
;expected

2.) To check if the queries result in a dataset is empty (that is the
queries did not find a match) , can you check it against Null like
below...

if (dataSet1 == NULL)
{
textBox.Text += "Hey";
}
Your response will be greatly appreciated.

Thanks
tiger






Reply With Quote
  #2  
Old   
pvdg42
 
Posts: n/a

Default Re: SQL in C#: How to - 2 questions - 11-03-2005 , 09:41 AM







"Jim Underwood" <james.underwood (AT) fallonclinic (DOT) com> wrote

Quote:
First, I use vb.net, but the two are pretty similar so this should work
anyway...

1. some suggestions...
First, put your SQL into a string variable, then pass the variable as the
command text. This makes no difference technically, but makes the code
easier to read and simplifies debugging.
Second, output the SQL String before you execute it. If you have trouble
you can cut and paste the string itself to execute it manually in your
database tool. Then you can fix the SQL outside of VisualStudio and
seperate SQL errors from C# errors.

Soem problems I see...
the field name "IP Address" is invalid becuase it contains a space. You
may
have the wrong field name, or you may need to place [] around the field
name
in your SQL.
you are usign your variables in your SQL without concatenating them. I
think
this should through an error when compiling. Add in the + for
concatenation.

Also, although concatenation works for your example, you want to use
parameters in place of concatenation or you run into security issues.
This
is nto really a problem for academic programs, but worht looking into once
you get the hang of basic queries.

string mySQL ;
mySQL = "select * from Customer where [IP Address] = '" + IP + "' OR City
=
'" + city + "'";
oleDbDataAdapter1.SelectCommand.CommandText = mySQL

2. To check if the dataset is empty try using is nothing (this works in
VB,
not certain about C#.
if (dataSet1 is nothing)


In C#, you need to check each table in the dataset for rows, something like
this...

if(dataSet11.Tables[0].Rows.Count == 0)

{

MessageBox.Show("No data");

}




Reply With Quote
  #3  
Old   
Sinu
 
Posts: n/a

Default Re: SQL in C#: How to - 2 questions - 01-30-2006 , 09:57 AM



The PLUS is missing in your query!
--
Shrinivas Reddy.
Systems Analyst
Satyam Computer Services Ltd.


"pvdg42" wrote:

Quote:
"Jim Underwood" <james.underwood (AT) fallonclinic (DOT) com> wrote in message
news:e8dAri$3FHA.696 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
First, I use vb.net, but the two are pretty similar so this should work
anyway...

1. some suggestions...
First, put your SQL into a string variable, then pass the variable as the
command text. This makes no difference technically, but makes the code
easier to read and simplifies debugging.
Second, output the SQL String before you execute it. If you have trouble
you can cut and paste the string itself to execute it manually in your
database tool. Then you can fix the SQL outside of VisualStudio and
seperate SQL errors from C# errors.

Soem problems I see...
the field name "IP Address" is invalid becuase it contains a space. You
may
have the wrong field name, or you may need to place [] around the field
name
in your SQL.
you are usign your variables in your SQL without concatenating them. I
think
this should through an error when compiling. Add in the + for
concatenation.

Also, although concatenation works for your example, you want to use
parameters in place of concatenation or you run into security issues.
This
is nto really a problem for academic programs, but worht looking into once
you get the hang of basic queries.

string mySQL ;
mySQL = "select * from Customer where [IP Address] = '" + IP + "' OR City
=
'" + city + "'";
oleDbDataAdapter1.SelectCommand.CommandText = mySQL

2. To check if the dataset is empty try using is nothing (this works in
VB,
not certain about C#.
if (dataSet1 is nothing)



In C#, you need to check each table in the dataset for rows, something like
this...

if(dataSet11.Tables[0].Rows.Count == 0)

{

MessageBox.Show("No data");

}




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.