我写了一些SQL语句并且把它放在了mysql.sql文件中,请问在C#中怎样执行我写在mysql.sql中的SQL语句啊?
谢谢le!!!

解决方案 »

  1.   

    当成string 读成字符串 然后用 sqlcommand 执行不一样么?
      

  2.   

    //从文件中提取SQL指令
    private string GetSqlFile(string FilePath)
    {
    string input;
    string Command
    StreamReader sr;

    sr = new StreamReader(FilePath,System.Text.Encoding.Default);
    while ((input=sr.ReadLine())!=null) 
    {
    Command = Command + " \n " + input;
    }
    sr.Close();
    return Command;
    }
      

  3.   

    对阿  肯定是在ado.net中运行这些相当于文本的一些命令而已。
      

  4.   

    using System;
    using System.Data;
    using System.Collections;
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.Diagnostics;namespace Zhzuo
    {

    public class ZZConsole
    {
    [STAThread]
    static void Main(string[] args)
    {
    //测试
    //string vv = ExeCommand("dir");
    //Console.WriteLine(vv);
    string sqlQuery = "osql.exe /uSa /p123 /s192.192.132.229 /dNorthWind /i yoursql.sql";
    string strRst = ExeCommand(sqlQuery);
    Console.WriteLine(strRst);
    Console.ReadLine();
    }

    public static string ExeCommand(string commandText)
    {
    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 strOutput = null;
    try
    {
    p.Start();
    p.StandardInput.WriteLine(commandText);
    p.StandardInput.WriteLine("exit");
    strOutput = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    p.Close();
    }
    catch(Exception e)
    {
    strOutput = e.Message;
    }
    return strOutput;
    }
    }
    }
    =====================
    使用osql执行Sql语句。用法: osql              [-U login id]          [-P password]
      [-S server]            [-H hostname]          [-E trusted connection]
      [-d use database name] [-l login timeout]     [-t query timeout]
      [-h headers]           [-s colseparator]      [-w columnwidth]
      [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
      [-L list servers]      [-c cmdend]            [-D ODBC DSN name]
      [-q "cmdline query"]   [-Q "cmdline query" and exit]
      [-n remove numbering]  [-m errorlevel]
      [-r msgs to stderr]    [-V severitylevel]
      [-i inputfile]         [-o outputfile]
      [-p print statistics]  [-b On error batch abort]
      [-X[1] disable commands [and exit with warning]]
      [-O use Old ISQL behavior disables the following]
      [-? show syntax summary]
    具体参考  
    http://www.588188.com/netbook/sqlserver2000/coprompt/cp_osql_1wxl.htm
    例如:
    osql.exe /uSa /p123 /s192.192.132.229 /dNorthWind /i yoursql.sqlms-help://MS.MSDNQTR.2003FEB.2052/vsintro7/html/vxwlkwalkthroughusingcustomactiontocreatedatabaseduringinstallation.htm
      

  5.   

    那如果我的SQL语句并不在SQL服务器上呢?
      

  6.   

    sql文件不用在sql server服务器上,就用sqlcmmand执行就行了。还有你也可以直接调用执行
    tsql 文件名.sql
    这种方式。