C#执行一个目录下所有sql文件

来源:互联网 发布:新浪微博域名二次修改 编辑:程序博客网 时间:2024/05/16 05:04
//点击按钮进行读取protected void LinkButton1_Click(objectsender, EventArgse)    {        stringpath = MapPath("/UploadFiles/ceshi/");        stringcon = ConfigurationManager.ConnectionStrings["Connection String"].ConnectionString;        string[] fileNames = Directory.GetFiles(path);        foreach(stringfile infileNames)        {            if(!ExecutionSql(file, con))            {                Response.Write("<script>alert('操作失败')</script>");            }            else            {                Response.Write("<script>alert('操作成功')</script>");            }        }    }//读取的方法    public static bool ExecutionSql(stringpath, stringconnectString)    {        SqlConnectionconnection = newSqlConnection(connectString);        SqlCommandcommand = newSqlCommand();        connection.Open();        command.Connection = connection;        using(StreamReaderreader = newStreamReader(path, Encoding.UTF8))        {            try            {                while(!reader.EndOfStream)                {                    StringBuilderbuilder = newStringBuilder();                    while(!reader.EndOfStream)                    {                        stringstr = reader.ReadLine();                        if(!string.IsNullOrEmpty(str) && str.ToUpper().Trim().Equals("GO"))                        {                            break;                        }                        builder.AppendLine(str);                    }                    command.CommandType = CommandType.Text;                    command.CommandText = builder.ToString();                    command.CommandTimeout = 300;                    command.ExecuteNonQuery();                }            }            catch(SqlException)            {                returnfalse;            }            finally            {                command.Dispose();                connection.Close();                connection.Dispose();            }        }        returntrue;    }

原创粉丝点击