Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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# 在SQL Server Management Studio中创建存储过程时出现语法错误_C#_Sql_Sql Server_Wpf - Fatal编程技术网

C# 在SQL Server Management Studio中创建存储过程时出现语法错误

C# 在SQL Server Management Studio中创建存储过程时出现语法错误,c#,sql,sql-server,wpf,C#,Sql,Sql Server,Wpf,目前,我正在开发一个清单系统,该系统允许客户端自定义自己的SQL Server表 用户将命名其列,然后指定要使用的数据类型。在他们对自己的表格感到满意之后,他们单击SubmitTable按钮来创建自己的表格。然后,C#将通过运行SQL Server存储过程为用户创建一个唯一的表 SqlConnection connection = new SqlConnection(Constants.conn); connection.Open(); string query = "EXEC Create

目前,我正在开发一个清单系统,该系统允许客户端自定义自己的SQL Server表

用户将命名其列,然后指定要使用的数据类型。在他们对自己的表格感到满意之后,他们单击SubmitTable按钮来创建自己的表格。然后,C#将通过运行SQL Server存储过程为用户创建一个唯一的表

SqlConnection connection = new SqlConnection(Constants.conn);
connection.Open();

string query = "EXEC CreateTable @TableName='"+ UserAccountInfo.Username + "InventoryTable'";

SqlCommand command = new SqlCommand(query, connection);
command.ExecuteReader();
List<string[]> list = new List<string[]>();

foreach (UserTable ut in StackTable.Children)
{
    string dataType = "";

    switch (ut.DataType.Text)
    {
        case "Text": dataType = "Varchar(100)"; break;
        case "Number": dataType = "INT"; break;
        case "Boolean": dataType = "Varchar(5)"; break;
    }

    string[] vs = {"[" + ut.Name.Text + "]", dataType};
    list.Add(vs);
}

foreach (string[] s in list)
{
    string query2 = "EXEC SelectAllCustomers @Name = '" + s[0] + "', @DataType = '" + s[1] + "';";

    SqlCommand command2 = new SqlCommand(query, connection);
    command2.ExecuteReader();
}
然后使用foreach循环,通过查看堆栈面板“StackTable”子级,并将UserTable“Name”和“DataType”的属性分配给字符串数组列表,迭代添加的每个列(类UserTable是我的自定义WPF UserControl)。然后,我在整个列表中迭代并执行第二个存储过程,该过程改变了先前创建的表

我相信这一切都会奏效!!我的问题和问题在于我的存储过程

SqlConnection connection = new SqlConnection(Constants.conn);
connection.Open();

string query = "EXEC CreateTable @TableName='"+ UserAccountInfo.Username + "InventoryTable'";

SqlCommand command = new SqlCommand(query, connection);
command.ExecuteReader();
List<string[]> list = new List<string[]>();

foreach (UserTable ut in StackTable.Children)
{
    string dataType = "";

    switch (ut.DataType.Text)
    {
        case "Text": dataType = "Varchar(100)"; break;
        case "Number": dataType = "INT"; break;
        case "Boolean": dataType = "Varchar(5)"; break;
    }

    string[] vs = {"[" + ut.Name.Text + "]", dataType};
    list.Add(vs);
}

foreach (string[] s in list)
{
    string query2 = "EXEC SelectAllCustomers @Name = '" + s[0] + "', @DataType = '" + s[1] + "';";

    SqlCommand command2 = new SqlCommand(query, connection);
    command2.ExecuteReader();
}
List List=新列表();
foreach(StackTable.Children中的UserTable ut)
{
字符串数据类型=”;
开关(ut.DataType.Text)
{
案例“Text”:dataType=“Varchar(100)”;中断;
案例“编号”:dataType=“INT”;中断;
大小写“Boolean”:dataType=“Varchar(5)”;break;
}
字符串[]vs={“[”+ut.Name.Text+“]”,数据类型};
列表。添加(vs);
}
foreach(列表中的字符串[]s)
{
字符串query2=“EXEC SelectAllCustomers@Name=”+s[0]+“,@DataType=”+s[1]+“;”;
SqlCommand command2=新的SqlCommand(查询、连接);
command2.ExecuteReader();
}
我的数据库模式由名为
InventoryDatabase
的基础数据库组成,在名为
LoginInfo
的表中存储用户信息,其余的表将是我们在上面创建的表。问题是,我的存储过程抛出语法错误


您必须使用动态SQL。下面是一个让您开始学习的示例:

create or alter procedure CreateTable @TableName varchar(100)
as
begin
     declare @sql nvarchar(max) = 
     concat('create table ',quotename(@TableName),' AccountId int primary key identity(1,1)');
     exec (@sql);
end

请注意,名称通过
quotename
函数传递,以防止SQL注入。

选项1:将SQL脚本准备为字符串,该字符串可以创建或更改C代码中的表,然后使用
ExecuteOnQuery
方法执行准备好的字符串


选项2:通过使用动态SQL概念,在存储过程中准备创建/更改表脚本,然后执行

您不能说
alter table@TableName
-请阅读动态SQL和SQL注入。它是
存储的
(不是
存储
)。变量不能用于替换对象的名称;当然,没有动态SQL也不行。但我强烈建议您也不要走这条路,因为很明显,您没有意识到SQL注入的巨大危险。你在这里的真正目标是什么?