Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ASP.NET将数据插入本地SQL Server数据库_C#_Asp.net - Fatal编程技术网

C# 从ASP.NET将数据插入本地SQL Server数据库

C# 从ASP.NET将数据插入本地SQL Server数据库,c#,asp.net,C#,Asp.net,我正在尝试将ASP.NET中的数据插入本地SQL Server数据库。我跟在后面。也许你可以试着先看视频。我遵循完全相同的流程 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <h

我正在尝试将ASP.NET中的数据插入本地SQL Server数据库。我跟在后面。也许你可以试着先看视频。我遵循完全相同的流程

代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
    .auto-style1 {
        text-align: center;
    }
    .auto-style2 {
        width: 100%;
    }
    .auto-style3 {
        width: 183px;
    }
    .auto-style4 {
        width: 183px;
        height: 21px;
    }
    .auto-style5 {
        height: 21px;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

    <h2 class="auto-style1">insert data</h2>
    <br />

</div>
    <table class="auto-style2">
        <tr>
            <td class="auto-style4">FirstName :</td>
            <td class="auto-style5">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style3">LastName :</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style3">City :</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style3">&nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
            </td>
        </tr>
    </table>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" OnSelecting="SqlDataSource1_Selecting" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
</form>
</body>
</html>
当我尝试插入数据时,出现以下错误:

是一个SQL关键字,您应该能够使用

[Table] 
将表名与关键字区分开来

所以试着使用

SqlCommand cmd = new SqlCommand("insert into [Table] (fname, lname, city) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);

您的文本框输入可能包含转义字符串的值。您使用的方法容易受到sql注入攻击

例如: 如果textbox1.txt包含一个“字符”,它将中断查询,因为它将捕获该值

如果查看SqlCommand对象的command text属性,您可能会看到这一点。我强烈建议您查看该属性,并在谷歌上搜索一下sql注入。如果您在这些框中的任何框中输入的是“”;drop database;--”,则整个数据库都将被删除

这可能是因为您的输入未被净化或未正确传递到sql。

-您不应该将sql语句连接在一起-使用参数化查询来避免sql注入-签出
[Table] 
SqlCommand cmd = new SqlCommand("insert into [Table] (fname, lname, city) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);