开发者

Create a database with CreateDatabase method in LINQ & C#?

开发者 https://www.devze.com 2022-12-09 05:57 出处:网络
I\'m using LINQ to SQL. I wanna create a database with LINQ but the following exception has occurred :

I'm using LINQ to SQL. I wanna create a database with LINQ but the following exception has occurred : Illegal characters in path.

This is my code :

public static string DBFolder = Application.StartupPath + "\\SQL\\";
private void MainForm_Load(object sender, EventArgs e)
  开发者_运维问答  {
        if (!Directory.Exists(StaticVariables.DBFolder))
            Directory.CreateDirectory(StaticVariables.DBFolder);
        using (RezaRestaurant.SQL.DataClasses1DataContext dbc = new RezaRestaurant.SQL.DataClasses1DataContext())
        {
            if (!File.Exists(StaticVariables.DBFolder + dbc.Mapping.DatabaseName + ".mdf") && !dbc.DatabaseExists())
            {
                RezaRestaurant.SQL.DataClasses1DataContext db = new RezaRestaurant.SQL.DataClasses1DataContext(StaticVariables.DBFolder + dbc.Mapping.DatabaseName + ".mdf", dbc.Mapping.MappingSource);
                db.CreateDatabase();//Exception
            }
        }
    }

BTW : I was using UFT8 characters in database's tables, like this : اقلام_فروخته_شده

Could you please guide me ? Thanks.


StaticVariables.DBFolder contains one or more of the illegal Win32 path characters.

you can use

System.IO.Path.GetInvalidPathChars()
System.IO.Path.GetInvalidFileNameChars()

to check which characters are not allowed, and then remove those from your StaticVariables.DBFolder variable

also make sure that dbc.Mapping.DatabaseName doesn't contain any of these characters

0

精彩评论

暂无评论...
验证码 换一张
取 消