// ---------------------------- Common.GetConnectionString ------------------- public static string GetConnectionString(AutoCoder.BasePage InPage) { System.IO.StreamReader reader = null; string connString = null; string path = InPage.MapPath("ConnectString.txt"); try { try { reader = new System.IO.StreamReader(path); connString = reader.ReadLine( ); } catch (System.IO.IOException excp) { throw(new ApplicationException("GetConnectionString error. " + excp.Message)); } } finally { if (reader != null) { reader.Close( ); } } return(connString); }
// --------------------- LoadMaster.CreateTable ------------------------- // create the table in the database. public static void CreateTable(AutoCoder.BasePage InPage) { SqlConnection conn; SqlCommand cmd; conn = new SqlConnection(Common.GetConnectionString(InPage)); conn.Open(); try { cmd = new SqlCommand("DROP TABLE LOADMASTER", conn); cmd.ExecuteNonQuery(); } catch { } cmd = new SqlCommand("CREATE TABLE LOADMASTER ( " + "LoadId varchar(50), " + "RawUrl varchar(1000), " + "PageTitle varchar(80), " + "EntryTs DATETIME, " + "PRIMARY KEY( LoadId ))", conn); cmd.ExecuteNonQuery(); conn.Close(); }
// --------------------- LoadMaster.CheckTable ------------------------- // check for table existance. return true if table exists. public static bool CheckTable(AutoCoder.BasePage InPage) { SqlCommand cmd = null; SqlConnection conn = null; SqlDataReader reader = null; bool doesExist = false; try { conn = Common.OpenConnection(InPage); cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@LoadId", "abc")); cmd.CommandText = "SELECT * from LoadMaster WHERE LoadId=@LoadId"; reader = cmd.ExecuteReader(); doesExist = true; } catch (SqlException) { doesExist = false; } finally { if (reader != null) { reader.Close(); } if (conn != null) { conn.Close(); } } return(doesExist); }
// ---------------------------- ArticleMaster.Add ------------------------ // assign ArticleName and run sql insert for the fields stored in the class. public void Add(AutoCoder.BasePage InPage) { SqlConnection conn = null; try { SqlCommand cmd = new SqlCommand(); conn = new SqlConnection(Common.GetConnectionString(InPage)); conn.Open(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@ArticleName", mArticleName)); cmd.Parameters.Add(new SqlParameter("@ArticleTitle", mArticleTitle)); cmd.Parameters.Add(new SqlParameter("@SummaryDocFile", mSummaryDocFile)); cmd.Parameters.Add(new SqlParameter("@FullDocFile", mFullDocFile)); cmd.CommandText = "Insert ARTICLEMASTER ( " + "ArticleName, ArticleTitle, SummaryDocFile, FullDocFile ) " + "Values( @ArticleName, @ArticleTitle, @SummaryDocFile, @FullDocFile )"; cmd.ExecuteNonQuery(); } finally { if (conn != null) { conn.Close(); } } }
// --------------------- ArticleMaster.CreateTable ------------------------- // create the table in the database. public static void CreateTable(AutoCoder.BasePage InPage) { SqlConnection conn; SqlCommand cmd; conn = new SqlConnection(Common.GetConnectionString(InPage)); conn.Open(); try { cmd = new SqlCommand("DROP TABLE ARTICLEMASTER", conn); cmd.ExecuteNonQuery(); } catch { } cmd = new SqlCommand("CREATE TABLE ARTICLEMASTER ( " + "ArticleName varchar(50), " + "ArticleTitle varchar(100), " + "SummaryDocFile varchar(50), " + "FullDocFile varchar(50) " + "PRIMARY KEY( ArticleName ))", conn); cmd.ExecuteNonQuery(); conn.Close(); }
// ----------------- Common.OpenConnection ----------------------------- public static SqlConnection OpenConnection(AutoCoder.BasePage InPage) { SqlConnection conn; conn = new SqlConnection(GetConnectionString(InPage)); conn.Open( ); return(conn); }
// ---------------------- BasePage.RedirectTo ------------------------- // redirect to this page from another page. public void RedirectTo(AutoCoder.BasePage frompage) { StringBuilder url = new StringBuilder( ); url.Append(mPageUrl); url.Append("?RefLoadId="); url.Append(frompage.PageLoadId); frompage.Response.Redirect(url.ToString( )); }
// --------------------- ArticleMaster.CheckTable ------------------------- // check for table existance. return true if table exists. public static bool CheckTable(AutoCoder.BasePage InPage) { SqlCommand cmd = null; SqlConnection conn = null; SqlDataReader reader = null; bool doesExist = false; bool rc; try { conn = Common.OpenConnection(InPage); cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from INFORMATION_SCHEMA.tables where table_name = " + "@TableName"; cmd.Parameters.Add(new SqlParameter("@TableName", "ARTICLEMASTER")); reader = cmd.ExecuteReader(); rc = reader.Read(); // not found exception. if (rc == false) { doesExist = false; } else { doesExist = true; } } finally { if (reader != null) { reader.Close(); } if (conn != null) { conn.Close(); } } return(doesExist); }
// --------------------- LoadMaster.Chain ------------------------------ public void Chain(AutoCoder.BasePage InPage, string InLoadId) { SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; bool rc; try { conn = new SqlConnection(Common.GetConnectionString(InPage)); conn.Open(); cmd = BuildChainCommand(conn, InLoadId); reader = cmd.ExecuteReader(); rc = reader.Read(); // not found exception. if (rc == false) { string msg = "Load master not found for LoadId " + InLoadId; throw(new ApplicationException(msg)); } // got a row. extract each column from the reader. mLoadId = reader.GetString(0); mRawUrl = reader.GetString(1); mPageTitle = reader.GetString(2); mEntryTs = reader.GetDateTime(3); } finally { if (reader != null) { reader.Close( ); } if (conn != null) { conn.Close( ); } } }
// --------------------- ArticleMaster.Chain ------------------------------ public void Chain(AutoCoder.BasePage InPage, string InArticleName) { SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; bool rc; try { conn = new SqlConnection(Common.GetConnectionString(InPage)); conn.Open(); cmd = BuildChainCommand(conn, InArticleName); reader = cmd.ExecuteReader(); rc = reader.Read(); // not found exception. if (rc == false) { string msg = "Article master not found for ArticleName " + InArticleName; throw(new ArticleMasterException(msg)); } // got a row. extract each column from the reader. mArticleName = reader.GetString(0); mArticleTitle = reader.GetString(1); mSummaryDocFile = reader.GetString(2); mFullDocFile = reader.GetString(3); } finally { if (reader != null) { reader.Close( ); } if (conn != null) { conn.Close( ); } } }
// ---------------------------- LoadMaster.Add ------------------------ // assign LoadId and run sql insert for the fields stored in the class. public void Add(AutoCoder.BasePage InPage) { SqlConnection conn = null; try { SqlCommand cmd = new SqlCommand(); conn = new SqlConnection(Common.GetConnectionString(InPage)); conn.Open(); // assign the LoadId. System.Guid guid; guid = System.Guid.NewGuid(); mLoadId = guid.ToString(); mEntryTs = DateTime.Now; cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@LoadId", mLoadId)); cmd.Parameters.Add(new SqlParameter("@RawUrl", mRawUrl)); cmd.Parameters.Add(new SqlParameter("@PageTitle", mPageTitle)); cmd.Parameters.Add(new SqlParameter("@EntryTs", mEntryTs)); cmd.CommandText = "Insert LoadMaster ( " + "LoadId, RawUrl, PageTitle, EntryTs ) " + "Values( @LoadId, @RawUrl, @PageTitle, @EntryTs )"; cmd.ExecuteNonQuery(); } finally { if (conn != null) { conn.Close(); } } }
// ------------------------ LoadMaster.NewPage -------------------------- public static string NewPage(AutoCoder.BasePage InPage, string InPageTitle) { LoadMaster lm = new LoadMaster( ); lm.RawUrl = InPage.Request.RawUrl; lm.PageTitle = InPageTitle; try { lm.Add(InPage); } // catch the add exception. ( probably the table does not exist. ) If the LoadMaster // table does not exist, try to create it, then try the add again. catch (SqlException excp) { if (AutoCoder.Common.IsObjectNotFoundExcp(excp) == false) { throw(excp); } LoadMaster.CreateTable(InPage); lm.Add(InPage); } return(lm.LoadId); }