using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; using FdbDataProvider; namespace DBTest { public partial class _Default : System.Web.UI.Page { Random r; SqlConnection sqlConn; FdbConnection fdbConn; //SqlCommand sqlCmd; //FdbCommand fdbCmd; static int maxIndex = 0; protected void Page_Load(object sender, EventArgs e) { r = new Random(); sqlConn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|" + @"\SqlServerDB.mdf;Integrated Security=True;User Instance=True"); //sqlConn = new SqlConnection(@"Data Source=SILEAS-PC\SQLFORFASTDB;initial catalog=test;uid=sa;pwd=ss1022"); fdbConn = new FdbConnection(@"DataSource=test.fdb", FdbConnection.DefaultInitDatabaseSize * 2, FdbConnection.DefaultInitIndexSize * 2, FdbConnection.DefaultExtensionQuantum * 2); //SqlDataSource1.SelectCommand = "SELECT * FROM test"; //SqlDataSource1.Select(new DataSourceSelectArguments()); //maxIndex = ListView1.Items.Count; if (TbxInsert.Text == "") { try { sqlConn.Open(); fdbConn.Open(); SqlCommand sqlCmd = sqlConn.CreateCommand(); sqlCmd.CommandText = "delete from persons"; FdbCommand fdbCmd = fdbConn.CreateCommand(); fdbCmd.CommandText = "delete from persons"; //where salary>=0"; sqlCmd.ExecuteNonQuery(); fdbCmd.ExecuteNonQuery(); maxIndex = 0; } catch (Exception ex) { LblError.Text = ex.Message; } finally { if (sqlConn.State == ConnectionState.Open) sqlConn.Close(); if (fdbConn.State == ConnectionState.Open) fdbConn.Close(); } } else { //maxIndex = Convert.ToInt32(TbxInsert.Text); } } protected void BtnSelect_Click(object sender, EventArgs e) { try { int loopCount = Convert.ToInt32(TbxCount.Text); int randName; DateTime startTime, endTime; TimeSpan d; sqlConn.Open(); LbxSelect.Items.Clear(); LbxDes.Items.Clear(); startTime = DateTime.Now; for (int i = 0; i < loopCount; ++i) { randName = r.Next(0, maxIndex); SqlCommand sqlCmd = sqlConn.CreateCommand(); //SqlDataSource1.SelectCommand = "SELECT * FROM test WHERE name=" + randName.ToString(); //SqlDataSource1.Select(new DataSourceSelectArguments()); sqlCmd.CommandText = "SELECT * FROM persons WHERE name=" + randName.ToString(); //sqlCmd.ExecuteNonQuery(); //LbxSelect.Items.Add(i.ToString()); SqlDataReader reader = sqlCmd.ExecuteReader(); while (reader.Read()) { LbxSelect.Items.Add(reader["name"].ToString()); //LbxDes.Items.Add(reader["address"].ToString()); } reader.Close(); } endTime = DateTime.Now; d = endTime - startTime; LblTime.Text = d.Seconds.ToString() + "秒" + d.Milliseconds.ToString() + "毫秒"; LblError.Text = "SQL Server select successfully!"; } catch (Exception ex) { if (TbxCount.Text == "") LblError.Text = "请输入查询次数!"; else LblError.Text = ex.Message; } finally { sqlConn.Close(); } } protected void BtnInsert_Click(object sender, EventArgs e) { try { int count = Convert.ToInt32(TbxInsert.Text), n = 0; DateTime startTime, endTime; TimeSpan d; SqlCommand sqlCmd = sqlConn.CreateCommand(); sqlConn.Open(); startTime = DateTime.Now; for (int i = 0; i < count; ++i) { //SqlCommand sqlCmd = sqlConn.CreateCommand(); sqlCmd.CommandText = "insert into persons(name,salary,address) " + "values(" + i.ToString() + "," + i.ToString() + ",'" + TbxDes.Text + "')"; //sqlCmd.CommandText = @"insert into persons(name,salary,address) " // + @"values(@name,@salary,@address)"; ////if (i == 0) ////{ // sqlCmd.Parameters.Add("name", SqlDbType.Int); // sqlCmd.Parameters.Add("salary", SqlDbType.Real); // sqlCmd.Parameters.Add("address", SqlDbType.NChar); ////} //sqlCmd.Parameters["name"].Value = i; //sqlCmd.Parameters["salary"].Value = i; //sqlCmd.Parameters["address"].Value = "'"+TbxDes.Text+"'"; n += sqlCmd.ExecuteNonQuery(); //SqlDataSource1.InsertCommand = "INSERT INTO test(name, salary, description) " // + "VALUES (" + i.ToString() + ", " + i.ToString() + ", '" + TbxDes.Text + "')"; //SqlDataSource1.Insert(); } endTime = DateTime.Now; d = endTime - startTime; LblTime.Text = d.Seconds.ToString() + "秒" + d.Milliseconds.ToString() + "毫秒"; maxIndex = Convert.ToInt32(TbxInsert.Text); LblError.Text = "SQL Server Insert " + n.ToString() + " records successfully!"; //maxIndex += count; } catch (Exception ex) { LblError.Text = ex.Message; } finally { sqlConn.Close(); } } protected void BtnClr_Click(object sender, EventArgs e) { try { sqlConn.Open(); SqlCommand sqlCmd = new SqlCommand("DELETE from persons", sqlConn); int n = sqlCmd.ExecuteNonQuery(); //SqlDataSource1.DeleteCommand = "DELETE from test"; //SqlDataSource1.Delete(); LblError.Text = "SQL Server Clear " + n.ToString() + " records successfully!"; //maxIndex = 0; } catch (Exception ex) { LblError.Text = ex.Message; } finally { sqlConn.Close(); } } //public void FdbCreateTable(FdbConnection connection, FdbCommand command, string name, string[] attr, string[] attrType) //{ // try // { // for (int i = 0; i < attr.Length; ++i) // { // CLI.FieldType type; // switch (attr[i]) // { // case "int": type = CLI.FieldType.cli_int8; break; // case "real": type = CLI.FieldType.cli_real8; break; // case "string": type = CLI.FieldType.cli_asciiz; break; // } // command.Fields.Add(attr[i], type); // } // if (connection.CreateTable(name, command.Fields) >= 0) // throw new Exception("Cannot create table in FastDB"); // } // catch (Exception ex) // { // LblError.Text = ex.Message; // } //} protected void BtnInsertFdb_Click(object sender, EventArgs e) { try { int count = Convert.ToInt32(TbxInsert.Text), n = 0; DateTime startTime, endTime; TimeSpan d; FdbCommand fdbCmd = fdbConn.CreateCommand(); fdbConn.Open(); startTime = DateTime.Now; for (int i = 1; i <= count; ++i) { //FdbCommand fdbCmd = fdbConn.CreateCommand(); //fdbCmd.CommandText = @"insert into persons(name,salary,address) " // + @"values(%name,%salary,%address)"; ////if (i == 0) ////{ //fdbCmd.Parameters.Add("name", CLI.FieldType.cli_int8, CLI.FieldFlags.cli_indexed); //fdbCmd.Parameters.Add("salary", CLI.FieldType.cli_int8); //fdbCmd.Parameters.Add("address", CLI.FieldType.cli_asciiz); ////} //fdbCmd.Parameters["name"].asInt64 = i; //fdbCmd.Parameters["salary"].asInt64 = i; //fdbCmd.Parameters["address"].asString = TbxDes.Text; fdbCmd.CommandText = "Insert into persons(name,salary,address) " + "values(" + i.ToString() + "," + i.ToString() + ",'" + TbxDes.Text + "')"; n += fdbCmd.ExecuteNonQuery(); } endTime = DateTime.Now; d = endTime - startTime; LblTimeFdb.Text = d.Seconds.ToString() + "秒" + d.Milliseconds.ToString() + "毫秒"; maxIndex = Convert.ToInt32(TbxInsert.Text); LblError.Text = "FastDB Insert " + n.ToString() + " records successfully!"; //maxIndex += count; } catch (Exception ex) { LblError.Text = ex.Message; } finally { fdbConn.Close(); } } protected void BtnSelectFdb_Click(object sender, EventArgs e) { try { int loopCount = Convert.ToInt32(TbxCount.Text); int randName; DateTime startTime, endTime; TimeSpan d; LbxSelectFdb.Items.Clear(); LbxDesFdb.Items.Clear(); fdbConn.Open(); startTime = DateTime.Now; for (int i = 0; i < loopCount; ++i) { randName = r.Next(0, maxIndex); //SqlDataSource1.SelectCommand = "SELECT * FROM test WHERE name=" + randName.ToString(); //SqlDataSource1.Select(new DataSourceSelectArguments()); //LbxSelectFdb.Items.Add(randName.ToString()); //LbxDesFdb.Items.Add(TbxDes.Text); FdbCommand fdbCmd = fdbConn.CreateCommand(); fdbCmd.CommandText = "select * from persons where name=" + randName.ToString(); //fdbCmd.ExecuteNonQuery(); //LbxSelectFdb.Items.Add(i.ToString()); FdbDataReader reader = fdbCmd.ExecuteReader(); while (reader.Read()) { LbxSelectFdb.Items.Add(reader["name"].ToString()); //LbxDesFdb.Items.Add(reader["address"].ToString()); } reader.Close(); } endTime = DateTime.Now; d = endTime - startTime; LblTimeFdb.Text = d.Seconds.ToString() + "秒" + d.Milliseconds.ToString() + "毫秒"; LblError.Text = "FastDB Select successfully!"; } catch (Exception ex) { if (TbxCount.Text == "") LblError.Text = "请输入查询次数!"; else LblError.Text = ex.Message; } finally { fdbConn.Close(); } } protected void BtnClrFdb_Click(object sender, EventArgs e) { try { FdbCommand fdbCmd = fdbConn.CreateCommand(); fdbCmd.CommandText = "delete from persons where salary >= 0"; fdbConn.Open(); int n = fdbCmd.ExecuteNonQuery(); //SqlDataSource1.DeleteCommand = "DELETE from test"; //SqlDataSource1.Delete(); LblError.Text = "FastDB clear " + n.ToString() + " records successfully!"; //maxIndex = 0; } catch (Exception ex) { LblError.Text = ex.Message; } finally { fdbConn.Close(); } } } }