C # .NET database operations (transfer)

xiaoxiao2021-03-06  40

Original is http://blog.9cbs.net/ohiolee/archive/2005/02/02/tosqlserver.aspx

I. The database connection will be managed in the following files in the database connection. Web.config // does not understand this. It is best to learn well, I think it is useful, and .NET is of course the thing to use .NET.

// error message display, client display specified error page

// Log in the role, except for Windows, "Forms", "Passport", "NONE"

->

< / Database> // or above is two A method of adding an element in XML. By both methods can access the database information inside. However, the access method is different.

connection code: using System.Data.SqlClient; using System.Collections.Specialized; // operating namespacestring strConn xml required = (String) ((NameValueCollection) Context.GetConfig ( "system.web / database") ["strConn"]; // The first method of accessing the database information in XML string strConn = system.configuration.configurationSettings.appsettings ["nwind"]. TOSTRING (); // Corresponding second method operation code: 1 The database is operated through the stored procedure in the database, and the engineering is ideal through the intermediate layer. Can improve safety and overall performance.

using (SqlConnection conn = new SqlConnection (strConn)) {SqlCommand cmd = new SqlCommand ( "sp_SP", conn); cmd.CommandType = CommandType.StoredProcedure; // declaration is to call a stored procedure cmd.Parameters.Add ( "@ SNO" Sqldbtype.varchar, 9); cmd.parameters.add ("@ td", sqldbtype.smalldatetime); cmd.parameters.add ("@ s_note", sqldbtype.varchar, 100); // Add variable parameter cmd.Parameters ["@Sno"]. Value = sno.text; cmd.parameters ["@ TD"]. Value = checkbase.text; cmd.parameters ["@ S_Note"]. Value = snote.text; // give parameter payment Value conn.open (); if (cmd.executenon query ()! = 0) // Execute no return set okmsg.text = "Login completion"; else okmsg.text = "Login failed";} 2, directly output SQL statement String strsql = "update nwind.nuser.data set sno = '" sno.text; // SQL Server needs to indicate the database name of the table and the user name IF (CheckBase.Text! = "") strsql = "', Month ='" checkbase.text "/ 01"; strsql = "'where sno ='" sno.text "'"; conn.open (); sqlcommand cmd = new SQLCommand (strsql, conn); IF (cmd.executenonQuery ()! = 0) okmsg.text = "Login"; else okmsg.text = "Fail";} 3. Retriever SQLDataAdapter and DataSet Joint use, generally used to obtain special operations for obtaining data sets, private dataset getdata () ((NameValueCollection) ("System.Web / Database") ["StrConn") ["strconn" ]; using (SqlConnection conn = new SqlConnection (strConn)) {SqlCommand cmd = new SqlCommand ( "sp_CO", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add ( "@ place", SqlDbType.VarChar, 2);

CMD.Parameters ["@ Place"]. value = place.text; conn.open (); sqldataadapter Da = new sqldataadapter (); da.selectcommand = cmd; dataset ds = new dataset (); da.fill (DS, "VCO"); // You can store multiple data sheets like a local database, implement the features you want to achieve. Specifically, the processing of the table has a column deletion, and the processing between multiple tables. COUNT.TEXT = "A total of:" DS.TABLES ["VCO"]. Rount.count.tostring () "pieces"; return ds;}

} 4. When only use to read data, it is recommended to use SqlDataReader, read the speed, a lot of private void DataLoader () (NameValueCollection) context.getconfig ("System.Web / Database" )) [ "strConn"]; using (SqlConnection conn = new SqlConnection (strConn)) {SqlCommand cmd = new SqlCommand ( "sp_list", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add ( "@ no ", Sqldbtype.varchar, 6); cmd.parameters [" @ no "]. Value = no.text; conn.open (); using (sqlDataReader DR = cmd.executeReader ()) {if (Dr.Read () ) {C.Text = DR ["c"]. TOSTRING (); // Get data M.Text = DR ["MTC"]. ToString (); //} else okmsg.text = "No data "}

}

转载请注明原文地址:https://www.9cbs.com/read-55304.html

New Post(0)