Example: When you click the Button1 button, the data is inserted into the database.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Text;
Using System. Data. SqlClient;
Using System. Data;
Using System. Configuration;
Namespace ParaMeter
{
Public partial class Test: System. Web. UI. Page
{
Private string connectionStr; // The string connecting to the database
Private SqlConnection conDB; // Database Link
Private SqlTransaction _ trans; // transaction object
Protected void Page_Load (object sender, EventArgs e)
{
// ConnectionStr = ConfigurationSettings. deleettings ["constr"];
ConnectionStr = "server = 10.11.43.189 \ SQL2008; database = OA_WEB_DB; uid = sa; pwd = 123456 ";
ConDB = new SqlConnection (connectionStr );
}
Protected void button#click (object sender, EventArgs e)
{
StringBuilder strSql = new StringBuilder ();
StrSql. Append ("insert into [OA_WEB_DB]. [dbo]. [OA_RT_FileType] ([FileTypeName], [Deleted])");
StrSql. Append ("VALUES (@ fileName, @ delete )");
SqlParameter [] parameters = {
New SqlParameter ("@ fileName", SqlDbType. NVarChar, 100 ),
New SqlParameter ("@ delete", SqlDbType. Bit ),
};
Parameters [0]. Value = "file type ";
Parameters [1]. Value = false;
Bool IsSucc = ExecUpdateSql (strSql. ToString (), parameters );
If (IsSucc)
{
Label1.Text = "successfully inserted ";
}
Else
{
Label1.Text = "insertion failed ";
}
}
/// Execute an update statement
/// </Summary>
/// <Param name = "SQLString"> SQL statement to be executed. </Param>
/// <Param name = "parms"> execution parameter array </param>
/// <Returns> success returns True, failure returns False. </Returns>
Private bool ExecUpdateSql (string SQLString, params SqlParameter [] extends parms)
{
Using (SqlCommand cmd = new SqlCommand ())
{
Try
{
PrepareCommand (cmd, conDB, _ trans, SQLString, partition parms );
Int iret = cmd. ExecuteNonQuery ();
Return true;
}
Catch (System. Data. SqlClient. SqlException e)
{
Return false;
}
}
}
Private void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string plain text, SqlParameter [] partition parms)
{
If (conn. State! = ConnectionState. Open)
Conn. Open ();
Cmd. Connection = conn;
Cmd. CommandText = plain text;
If (trans! = Null)
Cmd. Transaction = trans;
Cmd. CommandType = CommandType. Text; // specify type;
If (partition parms! = Null)
{
Foreach (SqlParameter parameter in parameter parms)
{
If (parameter. Direction = ParameterDirection. InputOutput | parameter. Direction = ParameterDirection. Input )&&
(Parameter. Value = null ))
{
Parameter. Value = DBNull. Value;
}
Cmd. Parameters. Add (parameter );
}
}
}
}
}