sql - Connection string not working in C# windows application -
for reasons, unable establish data connection using connection string. doing below code
var connectionstring = configurationmanager.connectionstrings["connection"].connectionstring; sqlcommand cmd = new sqlcommand(); sqlconnection con = new sqlconnection(connectionstring); cmd.connection = connectionstring; cmd.commandtype = commandtype.storedprocedure; cmd.commandtext = " dbo.selectall"; sqlparameter param = new sqlparameter("@tradedate","201401"); param.direction = parameterdirection.input; param.dbtype = dbtype.string; cmd.parameters.add(param);
but reasons when initializing connection property command using cmd.connection = connectiostring
, is throwing exception follows
cannot implicitly convert type 'string' 'system.data.sqlclient.sqlconnection'
you're confusing connection string needed connect database , actual sqlconnection.
try (for specific code):
cmd.connection = con;
according msdn here proper example:
private static void createcommand(string querystring, string connectionstring) { using (sqlconnection connection = new sqlconnection(connectionstring)) { sqlcommand command = new sqlcommand(querystring, connection); command.connection.open(); command.executenonquery(); } }
link original article: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx
Comments
Post a Comment