我写了一些SQL语句并且把它放在了mysql.sql文件中,请问在C#中怎样执行我写在mysql.sql中的SQL语句啊?
谢谢le!!!
谢谢le!!!
解决方案 »
- comboBox控件
- 万能的csdn,谁来拯救我--mvc连接discuz!NT开源论坛
- 关于access读写wav文件的问题
- 数据库连接字符的问题
- 请问几个数据库的基本问题?
- C#中,如何动态添加控件?
- 为什么myAdapter.Update(ds,strTblName);不能把DATASET的数据增加到数据库里
- .NET部署(急急)
- VS2005中如何像SharpDevelop那样设置标准头文件?
- installer调用的form无法打开openfiledialog
- 子窗体刷新另一个子窗体的datagrid???winform
- 我用HttpWebRequest或取一张网页代码的时候,出现问题,那个URL是:http://www.test.com/test.php?class=大分类 网页是通过class来动态输出
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;
}
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
tsql 文件名.sql
这种方式。