【问题标题】:How to Insert In .net core?如何插入.net核心?
【发布时间】:2019-09-19 21:10:48
【问题描述】:

我想使用 ssms 插入数据,但不使用 .net core 在 ssms 中插入数据??

插入代码,WebForm1.aspx.cs:

namespace InsertUpdateDelete.scripts
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void btninsert_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            string address = txtAddress.Text;
            Insert_Click(name, address);
        }

        void Insert_Click(string name, string address)
        {
            SqlConnection con = new SqlConnection(connstring);

            string query = "Insert into DemoInUpDelete (Name, Address) values (@Name,@Address)";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.CommandType = CommandType.Text;
            //Pass values to Parameters
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Address", address);
            try
            {
                con.Open();
                int validateOperation = cmd.ExecuteNonQuery();
                if (validateOperation > 0)
                {
                    //Message insert succesfully
                }
                else
                {
                    //Error
                }
            }
            catch (SqlException e)
            {
                //Exception
            }
            finally
            {
                con.Close();
            }

        void Update_Click(object sender, EventArgs e)
        {
               //update
        }

        void Delete_Click(object sender, EventArgs e)
        {
                   //delete
        }


    }
}



WebForm.aspx:


<body>


    <form id="form1" runat="server">

    <table>
         <tr>  
            <td>Name:</td>  
            <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>  
        </tr>  

        <tr>  
            <td>Address:</td>  
            <td><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>  
            <td> </td>  
        </tr>  

        <tr>  
            <td>  
               <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />
               <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="Update_Click" />  
               <asp:Button ID="btnCancel" runat="server" Text="Delete" OnClick="Delete_Click" />
            </td>  
        </tr>  
  </table>

    </form>
</body>

我是新 in.net 如何在 sql server SQL Server Management Studio 中插入数据? 我正在创建一个简单的新项目但不插入数据?? 数据库: 我正在创建数据库和 Id(primarykey) name(varchar(20) address(varchar(20)

.net 报错

SqlConnection con = new SqlConnection(connstring); connstring 没有 存在当前上下文

web.config.cs:

<configuration>

  <connectionStrings>
    <add name="ConnStringName" connectionString="Data Source= DESKTOP-U3PB1TF\SA; Integrated Security=true;Initial Catalog= InUpDelete; uid=sa; Password=admin@123; " providerName="System.Data.SqlClient" />
  </connectionStrings>

</connectionStrings>

【问题讨论】:

  • 您缺少 SqlCommand! SqlConnection 被注释掉了,基本上你需要遵循 C# 中的 SQL 插入指南。请遵循适当的教程,因为您的 SQL 是可注入的。我会尝试找到一个重复的。
  • 除了缺少 SqlCommand,从不 连接用户输入以运行命令,改为使用查询参数(另请参阅:general information on SQL Injection
  • 重新思考这个问题没有意义。 ASP.NET Core 不支持 WebForms,微软团队表示他们没有计划将 webforms 移植到 asp.net core。

标签: asp.net-core .net-core


【解决方案1】:

@Jeremy Thompson 表示你错过了插入操作代码

以下代码可能对您有所帮助。

aspx代码:

 <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />  

C#代码:

 protected void btninsert_Click(object sender,EventArgs e)
 {
   string name=txtName.Text;
   string address=txtAddtess.Text;
   Insert_Click(name,address);
 }


 void Insert_Click(string name,string address)
    {
        string connstring=System.Configuration.ConfigurationManager.
ConnectionStrings["ConnStringName"].ConnectionString;

        SqlConnection con = new SqlConnection(connstring);

        string query = "Insert into DemoInUpDelete (Name, Address) values (@Name,@Address)";           
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType=CommandType.Text;            
        //Pass values to Parameters
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Price", address);         
        try
        {
            con.Open();
            int validateOperation=cmd.ExecuteNonQuery();
    if(validateOperation>0)
    {
        //Message insert succesfully
            }
            else
            {
             //Error
             }               
        }
        catch (SqlException e)
        {
           //Exception
        }
        finally
        {
            con.Close();               
        }
}

【讨论】:

  • Asp.net 说“Insert_Click”没有重载匹配委托“EventHandler”
  • 你有类似 Insert_Click(object sender,EventArgs e){} 或 btninsert_Click(object sender,EventAgs e) 的方法?
  • 感谢您的代码,它对初学者非常有帮助,很容易实现 Asp.net 给出错误 System.Data.dll 中发生了“System.Data.SqlClient.SqlException”类型的异常,但是未在用户代码中处理附加信息:用户“sa”登录失败。我认为连接字符串错误
  • 显示编辑答案并将连接字符串中的密码从 **** 替换为您的实际密码。
  • 也许这不是一个最佳实践,你必须在 web.config 文件中定义你的连接字符串,然后在访问它之后。
猜你喜欢
  • 1970-01-01
  • 2012-02-02
  • 2021-08-23
  • 1970-01-01
  • 2017-10-23
  • 2018-12-14
  • 2016-04-10
  • 2017-07-01
相关资源
最近更新 更多