| |

VerySource

 Forgot password?
 Register
Search
View: 6|Reply: 6

How to create SQLSERVER database table in C #

[Copy link]

3

Threads

8

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 China

Post time: 2020-3-10 18:30:01
| Show all posts |Read mode
All the tables of the SQLSERVER database have generated SQL scripts. How to create a database in C # and use the script to create the tables? consult!
Reply

Use magic Report

0

Threads

104

Posts

56

Credits

Newbie

Rank: 1

Credits
56

 China

Post time: 2020-6-3 09:00:02
| Show all posts
private bool CreateDB( ref SqlConnection sqlConn)

        {

            string strQuery;

            string[] strComms;

            bool blnBreaked = false;

            if( ReadSQLFromFile( out strQuery))

            {

                strQuery = strQuery.Replace( "GO/r/n", ";" );

                strComms = strQuery.Split(';' );

 

                SqlCommand sqlComm = new SqlCommand();

                sqlComm.Connection = sqlConn;

 

                foreach( string strComm in strComms)

                {

                    sqlComm.CommandText = strComm;

                    try

                    {

                        sqlComm.ExecuteNonQuery();

                    }

                    catch( SqlException sqlErr)

                    {

                        MessageBox.Show( sqlErr.Message );

                        blnBreaked = true;

                        break;

                    }

                    catch{

                        blnBreaked = true;

                        break;

                    }

                }

               

                sqlComm.Dispose();

                if( !blnBreaked) return true;

            }

            return false;

        }

 

       Originally for simplicity of execution, all database script commands were executed at once, and ";" was used to replace the original "GO". Such an operation would make the database script containing StoreProcedure unable to execute successfully. The way to improve is to split the original script command set, and then execute each command one by one. Although it seems that this method is slightly less efficient, it is not always true, and the effect achieved is the same as the original method.

 

       Another point to note is that when the database creation fails, it is necessary to delete this incomplete database. In fact, the method is very simple, just execute the following script.

       use master

GO

      

if exists (select * from sysdatabases where name='mytest')

            drop database mytest

GO
Reply

Use magic Report

0

Threads

52

Posts

34

Credits

Newbie

Rank: 1

Credits
34

 China

Post time: 2020-6-3 13:15:01
| Show all posts
Either use cmd to execute the sql statement or call osql to execute the script
Reply

Use magic Report

3

Threads

8

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 China

 Author| Post time: 2020-6-3 22:45:01
| Show all posts
Code does not compile 555
Reply

Use magic Report

3

Threads

8

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 China

 Author| Post time: 2020-6-4 21:15:01
| Show all posts
How to call osql to execute a script?
Reply

Use magic Report

0

Threads

104

Posts

56

Credits

Newbie

Rank: 1

Credits
56

 China

Post time: 2020-6-6 17:15:01
| Show all posts
No problem with the code!
Call osql as follows:
If it is emphasized to create a database in the program, start a cmd.exe process to execute the database script file to create the database. See also:
// <summary>
/// Call Osql.exe to execute the database building script
/// </summary>
/// <param name="UserName">Database access user name</param>
/// <param name="Pwd">Database access password</param>

private void CreateDataBase (string UserName, string Pwd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string Path = Application.StartupPath.ToString();
string Parameter = "osql.exe -U "+ UserName +" -P "+ Pwd +" -S wangxiangmin -i "+ Path + @"\ICMS.sql";

try

{

p.Start();
p.StandardInput.WriteLine(Parameter);
p.StandardInput.WriteLine("exit");
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
}

catch(Exception e)
{

MessageBox.Show(e.Message);
               
this.Close();
}

}
Reply

Use magic Report

3

Threads

8

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 China

 Author| Post time: 2020-6-6 23:15:01
| Show all posts
to苹果小刀
The execution code prompts me: Error 1 The name "ReadSQLFromFile" does not exist in the current context
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list