Home>

Here to introduce the use of c#to access the sql server database. There are several classes involved here:sqlconnection class, sqlcommand class, sqldatareader class. The sqlconnection class is used to establish a connection with the database.The sqlcommand class is used to create a sql command. The sqldatareader object is the result of executing a query that returns the result set of the sqlcommand object.

Here is some code:

/* Where server represents the server,"." Means local server,database represents the database,uid represents the username of the connection,pwd ratio means password * /
string strdatabase="server=.;database=library;uid=sa;pwd=123456;";
sqlconnection conn=new sqlconnection (strdatabase);
string sqlstatement="select * from reader";
sqlcommand sqlcmd=new sqlcommand (sqlstatement, conn);//Set parameters
conn.open ();
sqldatareader sdr=sqlcmd.executereader ();//Execute SQL statement
int cols=sdr.fieldcount;//Get the number of columns in the result row
object [] values ​​= new object [cols];
while (sdr.read ())
{
  sdr.getvalues ​​(values);//values ​​save a row of data
  for (int i=0;i<values.length;i ++)
  {
   console.write (values ​​[i] .tostring () + "");
  }
  console.writeline ();
}
sdr.close ();
conn.close ();
c
  • Previous Introduction to ASP Basics Part 4 (Script Variables, Functions, Procedures, and Conditional Statements)
  • Next beautify form with jqTransform