Get the App
SLTechnology News&Howtos  ›  Development  › 

How to insert data into the database when you click the Button button

Shulou Source: shulou.com Published: 2022-06-03 05:43:11 11月17日 Update

Editor to share with you how to click the Button button to insert the data into the database, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Example: insert the data into the database when you click the Button1 button.

The 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; / / string that links to the database

Link to private SqlConnection conDB; / / database

Private SqlTransaction _ trans; / / transaction object

Protected void Page_Load (object sender, EventArgs e)

{

/ / connectionStr = ConfigurationSettings.AppSettings ["constr"]

ConnectionStr = "server=10.11.43.189\\ SQL2008;database=OA_WEB_DB;uid=sa;pwd=123456"

ConDB = new SqlConnection (connectionStr)

}

Protected void Button1_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 = "inserted successfully"

}

Else

{

Label1.Text = "insert failed"

}

}

/ / execute an update statement

/ / /

/ / SQL statement to be executed.

/ / execute parameter array

/ / return True for success and False for failure.

Private bool ExecUpdateSql (string SQLString, params SqlParameter [] cmdParms)

{

Using (SqlCommand cmd = new SqlCommand ())

{

Try

{

PrepareCommand (cmd, conDB, _ trans, SQLString, cmdParms)

Int iret = cmd.ExecuteNonQuery ()

Return true

}

Catch (System.Data.SqlClient.SqlException e)

{

Return false

}

}

}

Private void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter [] cmdParms)

{

If (conn.State! = ConnectionState.Open)

Conn.Open ()

Cmd.Connection = conn

Cmd.CommandText = cmdText

If (trans! = null)

Cmd.Transaction = trans

Cmd.CommandType = CommandType.Text;//cmdType

If (cmdParms! = null)

{

Foreach (SqlParameter parameter in cmdParms)

{

If ((parameter.Direction = = ParameterDirection.InputOutput | | parameter.Direction = = ParameterDirection.Input) & &

(parameter.Value = = null))

{

Parameter.Value = DBNull.Value

}

Cmd.Parameters.Add (parameter)

}

}

}

}

}

The above is all the contents of the article "how to insert data into the database when clicking the Button button". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Data databases buttons articles success content statements links not much transactions code examples parameters most characters strings objects arrays files time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Microsoft Huawei OPPO Reno