110,536
社区成员
发帖
与我相关
我的任务
分享
public static void ExecuteSQLFile(String sqlFileName)
{
SqlConnection connecction = null;
try
{
FrSetDbConnectionInfo fr = new FrSetDbConnectionInfo();
if (fr.ShowDialog() == DialogResult.OK)
{
string str = string.Format("server={0};database={1};user={2};pwd={3};", fr.dbInfo.Ip, "master", fr.dbInfo.SqlLoginName, fr.dbInfo.SqlPwd);
connecction = new SqlConnection(str);
XmlHelp.SetConString(fr.dbInfo.Ip, "YiYouData", fr.dbInfo.SqlLoginName, fr.dbInfo.SqlPwd);
}
else
{
return;
}
connecction = new SqlConnection(DbHelperSQL.connectionString);
SqlCommand command = connecction.CreateCommand();
connecction.Open();
using (FileStream stream = new FileStream(sqlFileName, FileMode.Open, FileAccess.ReadWrite))
{
StreamReader reader = new StreamReader(stream, Encoding.Default);
StringBuilder builder = new StringBuilder();
String strLine = "";
while ((strLine = reader.ReadLine()) != null)
{
if (strLine.Trim().ToUpper() != @"GO")
{
builder.AppendLine(strLine);
}
else
{
command.CommandText = builder.ToString();
command.ExecuteNonQuery();
builder.Remove(0, builder.Length);
}
}
stream.Dispose();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (connecction != null && connecction.State != ConnectionState.Closed)
{
connecction.Close();
}
}
}