Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
MySqlCommand  com = new MySqlCommand(" Select P.productname from productmast P join userpermission U on P.pid=U.uid innerjoin adminusermast A on U.adminuserid = A.adminuserid where ((A.adminuserid = 2) OR (A.adminuserid <> 2)) AND (U.permissionstatus = true))" , myPConn);



I write this query now i want to store it in a string SQeury = com; but its not working So how i store it .
Posted
Updated 6-Oct-11 19:45pm
v3
Comments
Mehdi Gholam 7-Oct-11 1:51am    
Your query is already a string.
hitech_s 7-Oct-11 1:54am    
why you want store command object ?
i think your requirement is string the text for that you can use
this
string query =commandobject.CommandText;
Vimalrashamra 7-Oct-11 2:23am    
actually i want the result of the query .
I want to resultant productid
P.Salini 7-Oct-11 2:30am    
use
string res=com.executenonquery();

what u want to store in a string?
You want to store the sql query or entire com object...
If u r trying to store entire com object then
I think It is not possible
 
Share this answer
 
Yes it is not possible to store cmd object in string.
if you want to get query then use this.

C#
string query=cmd.CommandText;
 
Share this answer
 
Comments
Vimalrashamra 7-Oct-11 2:22am    
it gives me the query not the result of query
P.Salini 7-Oct-11 2:29am    
to get result write this code
string result=cmd.ExecuteNonQuery();
Hi,

try this

C#
string val=com.ExecuteScalar ().tostring();


It returns the result of your query after executing in sql

All the Best
 
Share this answer
 
If I get understand you correctly you want to store your SQL command i.e. "Select * from sysobjects" in a string?

if so you have 2 ways that you can use it

C#
string SQL_String = "select * from sysobjects";
sqlconnection cn = new sqlconnection("ConnectionString") ;
cn.open(); //OPen connection object
sqlcommand cmd = new sqlcommand(SQL_String, cn);


or

C#
string SQLString = "select * from sysobjects";
sqlconnection cn = new sqlconnection("ConnectionString");
cn.open;

slqcommand cmd = new sqlcommand();
cmd.connection = cn;
cmd.commandtext = SQLString;
cmd.CommandType = CommandType.Text();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900