Um.
What the heck is all that about?
You are passed a connection string:
public Connect(SqlConnection Sql,string connectingString)
You create a new SqlConnection object, overwriting the one that was passed in.
using (Sql = new SqlConnection())
YOu find out how long it is - inefficiently:
int count = 0;
count = connectingString.Length;
You create a new array of characters the same length:
char[] chr = new char[count];
You throw it away, and convert the original string to an array:
chr = connectingString.ToCharArray();
You then create a new string from the character array which is identical to the original string:
string sd = new string(chr);
You then us the new, identical string to the SqlConnection object, after converting a string to ... a string:
Sql.ConnectionString = sd.ToString();
You then exit the
using
block which Closes the connection and Disposes of it.
And then you exit the method, which throws away the only reference to the new (but deleted) SqlConnection object you created and opened ....
You do realize that is the same as this:
public Connect(SqlConnection Sql,string connectingString)
{
}
Except this version can't throw an exception for a bad SQL connection string...
Parameters in C# are passed by
value
not by
reference
: whatever you do within the method to a variable passed in by the caller is not reflected in external code.
So when you overwrite the value passed in as a parameter, the caller variable is not changed:
private int Foo(int bar)
{
bar = bar * 2;
}
...
int x = 666;
Foo(x);
Console.WriteLine(x);
Will always write "666".
So your external SqlConnection that you pass in doesn't get it's ConnectionString property set at all, and you get an error when you later try to open it.
Quote:
Sir you guided me well. i have written the modified code and the connection was successful. but regarding the command object there's a error. The error of initialisating the executeQuery() . I am supplying the code to see the code:-
public Command(ref string commandText,ref SqlCommand cmd)
{
SqlConnection conn=new SqlConnection();
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
string strConn = "";
if (cmd.CommandText.StartsWith("select"))
{
cmd.ExecuteReader();
cmd.Dispose();
}
else
{
cmd.ExecuteNonQuery();
cmd.Dispose();
}
}
You are just guessing, aren't you?
Why is the command string a
ref
parameter?
Why is the command object a
ref
parameter?
You don't modify either of them!
You create a connection, but never oper it or even use it, you create a SqlDataReader but throw it away - but that's OK because you Dispose the command it's associated with!
Stop guessing. Throw this whole load of rubbish in the bin, and sit down and think about what you are supposed to be doing, and what is a sensible way to do it. Because at the moment all you are doing is wasting your time typing this stuff as you clearly don't understand any part of what you are actually doing - and guesswork is a very, very slow way to get any project completed.
Go back to you course notes and read them again, then think about what you have been told. Don't bother to try and fix what you have so far, it's not worth saving!