Home > Net >  Save teach c # read the resource in the SQL script to the database
Save teach c # read the resource in the SQL script to the database

Time:10-12

SQL database scripts I've done well. Is to create a stored procedure

Originally I was open the database directly.
Now I want to make it EXE, let the user directly on the local SQL scripts executed directly. I don't have to send the script directly to users.
I now have such an EXE at hand, but the decompiled.


I think now is the resource files in the memory, and then execute, performed to remove. This silly
But the report as shown in figure error




Similar to the following file

CodePudding user response:

Excuse me this kind of method how to read the resource below the SQL script?
String sqlpath=txt_Path. Text;
String sql1=@ "-s" + sysset. Essentially a + "3-d" + sysset. Dbname + "-i " "+ sqlpath +" \ "";
//call the SQLCMD
//- u sa,
ProcessStartInfo info=new ProcessStartInfo (SQLCMD, sql1);
//disable the OS Shell
Info. UseShellExecute=false;
//ban pop-up window
Info. CreateNoWindow=true;
//hide Windows style
The info. The WindowStyle=ProcessWindowStyle. Hidden;
//the standard output
Info. RedirectStandardOutput=true;

The Process proc=new Process ();
Proc. StartInfo=info;
//start process
Proc. Start ();
MessageBox. Show (" SQL upgrade is complete!" , "tip");
}

CodePudding user response:

Write SQL scripts in resources, very strange, is the first time I saw)
Suggestions can be written in the button events, so there is a problem can also be the debug

CodePudding user response:

Can, execute in batch database backup before, completely no problem

CodePudding user response:

The first step to copy the SQL file to the project, right-click attribute is set to the embedded resource, basic can then use the following code,

 using System. IO; 
Using System. The Reflection;
Using System. The Text;

The namespace RockfallDbScriptResources
{
///& lt; Summary>
///rockfall monitoring database resource manager script
///& lt;/summary>
Public static class RockfallDbScriptManager
{
///& lt; Summary>
///database name
///& lt;/summary>
Public static string DatabaseName {get; The set; }

///& lt; Summary>
///the result of the database scripts available
///& lt;/summary>
///& lt; Param name="resourceTypeFullName & gt;" The type of resource full name & lt;/param>
///& lt; Returns>
Private static string GetDbScriptResource (string resourceTypeFullName)
{
String resourceResult=null;
Stream, Stream=Assembly. GetExecutingAssembly (.) GetManifestResourceStream (resourceTypeFullName);
If (stream!=null)
{
Using (StreamReader sr=new StreamReader (stream, Encoding. The Default))
{
ResourceResult=sr. ReadToEnd (). The Replace (" # _ $_DBNAME_ $_ #, "DatabaseName);
}
}
Return resourceResult;
}

Public static string GetDropDatabaseCommandText ()
{
Return GetDbScriptResource (" RockfallDbScriptResources. DropDatabase. SQL ");
}

Public static string GetCreateDatabaseCommandText ()
{
Return GetDbScriptResource (" RockfallDbScriptResources. Atian_Taiwan_Demo_CreateDB. SQL ");
}

Public static string GetCreateTableCommandText ()
{
Return GetDbScriptResource (" RockfallDbScriptResources. Atian_Taiwan_Demo_CreateTable. SQL ");
}
Public static string [] GetCreateViewCommandText ()
{
Return GetDbScriptResource (" RockfallDbScriptResources. Atian_Taiwan_Demo_CreateView. SQL "). The Replace (" \ r \ n \ r \ nGO "and"?" ). The Split ('? ');//deal with constant values in SQL
}

Public static string [] GetCreateTriggerCommandText ()
{
Return GetDbScriptResource (" RockfallDbScriptResources. Atian_Taiwan_Demo_CreateTrigger. SQL "). The Replace (" \ r \ n \ r \ nGO "and"?" ). The Split ('? ');//deal with constant values in SQL
}

Public static string [] GetCreatePagingCommandText ()
{
Return GetDbScriptResource (" RockfallDbScriptResources. Essentially - PROCEDURE_COMMONPAGING_V1. 1. SQL "). The Replace (" \ r \ n \ r \ nGO "and"?" ). The Split ('? ');//deal with constant values in SQL
}

Public static string GetInitializeCommandText ()
{
Return GetDbScriptResource (" RockfallDbScriptResources Atian_Taiwan_Demo_Init data. SQL ");
}

}
}


Note: database and table can be batch, views, triggers, stored procedures, can only perform one at a time, you can use the GO apart, and then replaced with multiple arrays, then loop execution can, can also write directly in each SQL file, note cannot contain GO keywords, ADO.NET is not allowed to have this, you can use the replace replace,

CodePudding user response:

There is a kind of method call OSQL process, but this will receive environmental impact,

CodePudding user response:

The
Quote: reference 4 floor XBodhi. Reply:

Are very thank you can read out
But as I wrote array execution times wrong
 
Stream, Stream=Assembly. GetExecutingAssembly (.) GetManifestResourceStream (" SQLEXEC. Resources. Statistical document number 20190710. The SQL ");
If (stream!=null)
{
Using (StreamReader sr=new StreamReader (stream, Encoding. The Default))
{
While (sr. Peek () & gt; 1)
{
VarLine=sr. ReadLine ();
//varLine=varLine. Replace (" (KMS) ", "/" + dbname + "");
If (varLine=="")
{
continue;
}
If (varLine!="GO" & amp; & VarLine!="go" & amp; & VarLine. Substring (0, 1)!="/")
{
CommandText +=varLine;
CommandText +="\ r \ n";
}
The else
{
Sqllist. Add (commandText);
CommandText="";
}
}
//the string line=sr. ReadToEnd ();
The sr. The Close ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related