Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report