如何使用asp.net在sql中存储数据 [英] how to store data in sql by using asp.net

查看:60
本文介绍了如何使用asp.net在sql中存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.SqlClient;

使用System.Configuration;



namespace empdetails

{

public partial class emp:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

$



}



protected void TextBox2_TextChanged(object sender,EventArgs e)

{



}



protected void TextBox3_TextChanged(object sender,EventArgs e)

{



}



protected int Button1_Click1(object sender,EventArgs e)

{

string str = ConfigurationSettings.AppSettings [rajesh]。ToString();



SqlConnection con = new SqlConnection(str);

con .Open();

string s =(insert into emp values(name = @ name,pasword = @ pasword,salary = @ salary,adress = @adress));

SqlCommand cmd = new SqlCommand(s,con);

cmd.Parameters.AddWithValue(@ name,TextBox1.Text);

cmd.Parameters .AddWithValue(@ pasword,TextBox2.Text);

cmd.Parameters.AddWithValue(@ salary,TextBox3.Text);

cmd.Parameters.AddWithValue (@adress,TextBox4.Text);



int result = cmd.ExecuteNonQuery();



con.Close();



返回结果;

}

}

}

解决方案

你有什么错误?

你可以参考下面的代码作为你的参考 -

  //  创建包含值的变量 
string strName = < span class =code-string> TestData;

// 将上述数据变量插入db-table
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [ ConnectionString的]的ToString());
SqlCommand cmd = new SqlCommand();

cmd.CommandText = INSERT INTO YourTableName(YourFirstNameColumnName)VALUES(@FirstName)

//使用FirstName变量并创建一个新的sql变量,用于sql命令文本
cmd.Parameters.Add(
@FirstName ,SqlDbType.NVarChar).Value = FirstName;

cmd.Connection = conn;

conn。 Open();

cmd.ExecuteNonQuery();

conn.Close();


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace empdetails
{
public partial class emp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void TextBox2_TextChanged(object sender, EventArgs e)
{

}

protected void TextBox3_TextChanged(object sender, EventArgs e)
{

}

protected int Button1_Click1(object sender, EventArgs e)
{
string str = ConfigurationSettings.AppSettings["rajesh"].ToString();

SqlConnection con=new SqlConnection(str);
con.Open();
string s=("insert into emp values(name=@name,pasword=@pasword,salary=@salary,adress=@adress)");
SqlCommand cmd=new SqlCommand(s,con);
cmd.Parameters.AddWithValue("@name",TextBox1.Text);
cmd.Parameters.AddWithValue("@pasword",TextBox2.Text);
cmd.Parameters.AddWithValue("@salary",TextBox3.Text);
cmd.Parameters.AddWithValue("@adress",TextBox4.Text);

int result=cmd.ExecuteNonQuery();

con.Close();

return result;
}
}
}

解决方案

What error you are gtetting?
You can refer below code for your refrence -

//Makes a variable that contains your value
string strName= "TestData";

//Inserts the above data variable into the db-table
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "INSERT INTO YourTableName (YourFirstNameColumnName) VALUES (@FirstName)

        //Uses the FirstName variable and creates a new sql variable for use in the sql commandtext
        cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = FirstName;

        cmd.Connection = conn;

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();


这篇关于如何使用asp.net在sql中存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆