更新时间:2023-01-29 11:13:44
using(SqlConnection sc = new SqlConnection()) { sc.ConnectionString = "Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True"; sc.Open(); using (SqlCommand myCommand = new SqlCommand( "INSERT INTO table (rollno, name,feedback) VALUES (@rollno, @name,@feedback)")) { myCommand.Parameters.AddWithValue("@rollno", '"+ txtboxrollnumber.Text +"'); myCommand.Parameters.AddWithValue("@name", '"+ txtboxname.Text +"'); myCommand.Parameters.AddWithValue("@feedback", '"+ txtboxfeedback.Text +"'); //... myCommand.ExecuteNonQuery(); //... } sc.Close(): }
http://***.com/questions/16215995/adding-textbox-values-to-an-sql-database-in-c-sharp[^]
:-)
Instead of calling query from asp.net you can write stored procedure for inserting,
CREATE PROCEDURE [dbo].[ADD_Data]
/*Type should be their column types from table*/
@Name varchar(MAX),
@RollNo varchar(MAX),
@Feedback varchar(MAX)
AS
BEGIN
INSERT INTO [dbo].[FeedBackTable] (Name , RollNo , Feedback )
VALUES (@Name , @RollNo , @Feedback )
END
Now below code will go in your Button click ,protected void Button1_Click(object sender, EventArgs e) { if (Request.QueryString["Name"] != null && Request.QueryString["RollNumber"] != null) { SqlConnection con = new SqlConnection(connectionString); SqlCommand com = new SqlCommand("ADD_Data", con); com.Parameters.AddWithValue("@Name", Request.QueryString["Name"].ToString()); com.Parameters.AddWithValue("@RollNo", Request.QueryString["RollNumber"].ToString()); com.Parameters.AddWithValue("@Feedback", txtboxFeedback.Text); com.CommandType = CommandType.StoredProcedure; try { con.Open(); com.ExecuteNonQuery(); } catch (Exception) { throw; } finally { if (con.State == ConnectionState.Open) con.Close(); } } }