Mysql 使用asp.net将值插入SQL Server

Mysql 使用asp.net将值插入SQL Server,mysql,asp.net,Mysql,Asp.net,表单按钮: <asp:Button ID="Button1" runat="server" Text="GO" onclick="Button1_Click" /> 将值(生成的id)插入SQL Server数据库的简单代码是什么?当触发按钮1\u Click时,应将生成的id插入到tbl\u批处理中 我是否应该向web.config添加任何内容(如db连接) 您不能在web.config文件中执行CRUD操作,但我有一个简单的解决方案供您使用SqlDataSource。遵循以下步

表单按钮:

<asp:Button ID="Button1" runat="server" Text="GO" onclick="Button1_Click" />
  • 将值(生成的id)插入SQL Server数据库的简单代码是什么?当触发
    按钮1\u Click
    时,应将生成的id插入到
    tbl\u批处理中

  • 我是否应该向
    web.config
    添加任何内容(如db连接)


  • 您不能在web.config文件中执行CRUD操作,但我有一个简单的解决方案供您使用SqlDataSource。遵循以下步骤,以简单的方式完成你的任务

    我的Web.config中的连接字符串:

    <add name="Conn" connectionString="Server=David-PC;Database=DNN711;uid=sa;pwd=sa123;" providerName="System.Data.SqlClient"/>
    
    <asp:Button ID="Button1" runat="server" Text="GO" onclick="Button1_Click" />
    
        <asp:SqlDataSource ID="sdsInsert" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Conn %>" 
            InsertCommand="Insert into tbl_batch (GeneratedID) values (@GeneratedID)"
            InsertCommandType="Text"></asp:SqlDataSource> 
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        sdsInsert.InsertParameters.Add("GeneratedID", "123");
        sdsInsert.Insert();
    }
    
    如果你有任何问题,请告诉我

     Using con As SqlConnection = New SqlConnection("Server=localhost\SQLEXPRESS; Database=databasename; User Id=sa; Password=yourpassword;")
                Using cmd As SqlCommand = con.CreateCommand()
                    Using da As New SqlDataAdapter
                        con.Open()
    
                        cmd.CommandText = "insert into ..."
                        da.SelectCommand = cmd
                        Dim dt As New DataTable
                        da.Fill(dt)
    
                    End Using
                End Using
            End Using
    
     Using con As SqlConnection = New SqlConnection("Server=localhost\SQLEXPRESS; Database=databasename; User Id=sa; Password=yourpassword;")
                Using cmd As SqlCommand = con.CreateCommand()
                    Using da As New SqlDataAdapter
                        con.Open()
    
                        cmd.CommandText = "insert into ..."
                        da.SelectCommand = cmd
                        Dim dt As New DataTable
                        da.Fill(dt)
    
                    End Using
                End Using
            End Using