C# 使用Javascript和ASP.NET将数据插入SQL表

C# 使用Javascript和ASP.NET将数据插入SQL表,c#,javascript,asp.net,sql,sql-server,C#,Javascript,Asp.net,Sql,Sql Server,我使用Microsoft Visual Studio 2012作为平台,并创建了Web窗体项目 我在他的表文件夹中创建了数据库文件SimpleDB.mdf,我添加了一个名为Table的新表,该表有两列-id和Namestring。我尝试的是在从javascript函数调用服务器端函数时,将字符串数据插入该表的Name列 这是aspx.cs代码 using System; using System.Collections.Generic; using System.Linq; using Syst

我使用Microsoft Visual Studio 2012作为平台,并创建了Web窗体项目 我在他的表文件夹中创建了数据库文件SimpleDB.mdf,我添加了一个名为Table的新表,该表有两列-id和Namestring。我尝试的是在从javascript函数调用服务器端函数时,将字符串数据插入该表的Name列

这是aspx.cs代码

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

namespace ProjectWWW
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        [WebMethod]
        public static string InsertData(string ID){
            string source = "Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30";
            SqlConnection con = new SqlConnection(source); 
            {

               SqlCommand cmd = new SqlCommand("Insert into Table(Name) values('" + ID + "')", con);
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    return "True";
                }
            }
        }
}
这是aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ProjectWWW.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">   
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script>
        function CallMethod() {
            PageMethods.InsertData("hello", CallSuccess, CallError);
        }

        function CallSuccess(res) {
            alert(res);
        }

        function CallError() {
            alert('Error');
        }
    </script>
</head>

    <body>
        <header>        
        </header>       
        <div class="table"  id="div1" > </div>                      
        <form id="Form1" runat="server">
            <asp:Button id="b1" Text="Submit" runat="server" onclientclick="CallMethod();return false;"/>
            <asp:ScriptManager enablepagemethods="true" id="ScriptManager1" runat="server"></asp:ScriptManager>
        </form> 

    </body>


   </html>
因此,基本上,我希望在单击“提交”按钮时,表列名将填充Hello,但什么也没有发生,列保持为空。ull

表在T-SQL中是保留字,因此我建议您使用[]方括号将表括起来

试试这个:

SqlCommand cmd = new SqlCommand("Insert into [Table](Name) 
                                                   values('" + ID + "')", con);
using(SqlConnection con = new SqlConnection(source))
{
    using(SqlCommand cmd = new SqlCommand("Insert into [Table](Name)
                                                     values(@Name)", con))
    {            
        con.Open();
        cmd.Parameters.AddWithValue("@Name",ID);
        cmd.ExecuteNonQuery();
        return "True";
    }
}
建议:您的查询容易受到sql注入攻击。我建议您使用参数化查询来避免它们

试试这个:

SqlCommand cmd = new SqlCommand("Insert into [Table](Name) 
                                                   values('" + ID + "')", con);
using(SqlConnection con = new SqlConnection(source))
{
    using(SqlCommand cmd = new SqlCommand("Insert into [Table](Name)
                                                     values(@Name)", con))
    {            
        con.Open();
        cmd.Parameters.AddWithValue("@Name",ID);
        cmd.ExecuteNonQuery();
        return "True";
    }
}

你说它保持为空是什么意思?您正在尝试更新现有记录还是插入新记录?我用您的代码测试了它正在使用的sql查询更改为:Insert into[Table]Name values'+ID+'。放置try catch并存储cmd.ExecuteNonQuery的结果;在int变量中,检查是否存在任何错误。仍然没有发生任何事件函数CallMethod{PageMethods.InsertDatahello,CallSuccess,CallError;}警告错误原因?