First, I declare that I am a programming cainiao. This article is just a summary of my own!
Today, I have nothing to worry about. I wrote a piece of C #. Net code to connect to sqlserver. I found some information during the writing process and encountered some problems,
Let's talk less about the code first.
Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. data; <br/> using system. data. sqlclient; <br/> namespace test <br/> {<br/> public class dbconnection <br/> {<br/> Public static void getdbdata (out list <string []> rsltlist) <br/>{< br/> rsltlist = new list <string []> (); <br/> sqlconnection objconnection = NULL; <br/> try <br/> {<br/> string strconnection = "Persist Security info = false; user id = myid; Password = mypwd; initial catalog = northwind; server = mysqlserver; Connect timeout = 30 "; <br/> // myid is the user name <br/> // mypwd is the password <br/> // the data source that northwind uses for you, that is to say, the name of the database you want · <br/> // The server where the database is located (IP address can be written) <br/> objconnection = new sqlconnection (strconnection); <br/> objconnection. open (); <br/> // table_a is the retrieved table <br/> sqlcommand objsql = new sqlcommand ("select * From table_a"); <br/> objsql. connection = objconnection; <br/> sqldatareader myreader = objsql. executereader (); <br/> while (myreader. read () <br/>{< br/> string [] STR = new string [6]; <br/> // The following is a bunch of fields in Table table_a <br/> STR [0] = myreader. getstring (myreader. getordinal ("booknumber"); <br/> STR [1] = myreader. getstring (myreader. getordinal ("bookname"); <br/> STR [2] = myreader. getstring (myreader. getordinal ("publisher"); <br/> STR [3] = myreader. getstring (myreader. getordinal ("buyday"); <br/> STR [4] = myreader. getstring (myreader. getordinal ("country"); <br/> STR [5] = myreader. getstring (myreader. getordinal ("field"); <br/> rsltlist. add (STR); <br/>}< br/> finally <br/>{< br/> objconnection. close (); <br/>}< br/>}
1, string strconnection = "Persist Security info = false; user id = myid; Password = mypwd; initial catalog = northwind; server = mysqlserver; Connect timeout = 30 ";
This section should be some of the settings before connecting to the DB.
Including the user name, password, database name, server address, and so on.
If your SQL Server is set to Windows logon, you do not need to use the "user ID" and "password" methods to log on here, you need to use "trusted_connection = sspi" to log on.
The Code is as follows: String strconnection = "Persist Security info = false; Integrated Security = sspi; database = northwind; server = mysqlserver; Connect timeout = 30 ";
PS: persist Security info = false? (For details, refer to Google)
Persist Security
The info attribute indicates whether to save the security information. It can be simply understood as "Does ADO Save the password information after the database connection is successful ",
True indicates saving, and false indicates not saving.
2. Two problems encountered during code writing
Question:
System. invalidoperationexception
Is an exception for serverversion = 'objconnection. serverversion.
The specific content is: The operation is invalid and the connection is closed.
However, this exception is not throw. After objconnection. open () is executed,
Serverversion is displayed.
Think about it, isn't it an exception?
Because you are not open yet, people will of course report "The operation is invalid, the connection is closed" this "warning! (Guess, haha, don't hit me !)
Question B:
Executereader: the connection property has not been initialized.
So add: objsql. Connection = objconnection
Solve the problem.
Think about it. Otherwise, how does one sqlcommand know which connection I use and which dB I use ?!
Sometimes the code looks quite simple, but there are several problems when writing such a few codes.
It's just accumulation!
Remember Later!