如何在ASP.NET中创建新数据库? [英] How to create new database in ASP.NET?

查看:215
本文介绍了如何在ASP.NET中创建新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是为用户将添加到网站上的新品牌创建一个新数据库.我已经创建了执行此任务的代码,但它给了我这个异常: System.Data.dll中发生了类型为"System.Data.SqlClient.SqlException"的异常,但未在用户中处理代码附加信息:字符"附近的语法不正确.

我已经看过几次代码,但仍然找不到问题的根本原因.
我尝试过的事情:

 公共 静态  void  CreateGuitarBrandsDatabase(字符串品牌)
{
    SqlConnection createBrandData =  SqlConnection( @" );
    createBrandData.Open();
    SqlCommand cmdBrandData =  SqlCommand("  +品牌+ " ,createBrandData);
    cmdBrandData.ExecuteNonQuery(); // 异常似乎指向此处
    createBrandData.Close();
} 

解决方案

neck type应该为[neck type]necktype


作为对"解决方案1",您也有

"拾音器"
``桥式拾音器''


假设您想在列名称中保留空格.

脖子类型应该是[脖子类型]
颈部拾音器应该是[颈部拾音器]
桥拾音器应为[bridge pick]

我还建议在description = [description]中添加括号(不是问题)

另外,请查看文章 SQL注入和跨站点脚本 [ ^ ]如果有时间.


My goal is to create a new database for new brands that the user will add on the website. I''ve created a code for doing such task but it is giving me this exception: An exception of type ''System.Data.SqlClient.SqlException'' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near ''char''.

I already looked at the code several times but i still can''t find the root cause of the problem.

What I have tried:

public static void CreateGuitarBrandsDatabase(string brand)
{
    SqlConnection createBrandData = new SqlConnection(@"Data Source=Y560\SQLEXPRESS;Initial Catalog=GuitarItemsDB;Integrated Security=True");
    createBrandData.Open();
    SqlCommand cmdBrandData = new SqlCommand("CREATE TABLE guitarItem" + brand + "(id int,type char(50),model char(50),price float,image1 char(255),image2 char(255),description text,neck type char(100),body char(100), fretboard char(100),fret char(50),bridge char(100),neck pickup char(100),bridge pickup char(100),hardware_color char(50));", createBrandData);
    cmdBrandData.ExecuteNonQuery();//The exception seems to be pointing right here
    createBrandData.Close();
}

解决方案

neck type should be [neck type] or necktype


as an extension to ''Solution 1'', you also have

''neck pickup''
''bridge pickup''

which shouldnt have a space between them


Assuming you want to keep the space in the column name.

neck type should be [neck type]
neck pickup should be [neck pickup]
bridge pickup should be [bridge pickup]

I also suggest to add bracket to description = [description] (not an issue)

Also, please take a look at the article SQL Injection and Cross-Site Scripting[^] when you have time.


这篇关于如何在ASP.NET中创建新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆