「Chifu」的全部文章

字串練習,課堂作業

題目: 在特定的文章字串中,搜尋輸入的字串
條件1:若有符合的字串,將其索引值印出 (全部印出,並非印出第一個符合的索引值)
2:最後印出 總共有9個"的", 若輸入的字為"的"

text='在 Google I/O 2013 開發者大會上,除了 Samsung GALAXY S 4 的 Google 版本外,並未發表任何新產品;但許多消費者一定很期待,包括 Nexus 4、Nexus 7、Nexus 10 的後繼機種的消息。眼尖的網友或許會發現,在 Google I/O 2013 的介紹影片中,與 Nexus 4、Nexus 10 並列的官方圖片,中間那款並不是 Nexus 7,機身頂部與底部都是彎曲設計,極有可能是 Nexus 7 的後繼機種。不過,根據 VR-Zone 獲得的資訊顯示,次世代 Nexus 7 還是由 ASUS 代工,除了螢幕為 1920 x 1080 解析度 IPS 面板外,可能還會選擇 Qualcomm Snapdragon 800 四核心處理器,至於 NVIDIA Tegra 4 則因成本考量而被摒棄,這或許是二代 Nexus 7 至今還無法釋出的原因之一。'
findstr=input("請輸入要搜尋的字:")

ASP.NET SQLite Sample 上課範例程式碼

        void showdata()
        {
        //建立資料表
        SQLiteConnection conn = new SQLiteConnection("Data source=mydb.sqlite");
        // Open
        conn.Open();
        //要下任何命令先取得該連結的執行命令物件
        SQLiteCommand cmd = conn.CreateCommand();
        // 建立資料表
        cmd.Connection = conn;
        cmd.CommandText = "SELECT * FROM address_book";
        // 執行查詢塞入 sqlite_datareader
        SQLiteDataReader sqlite_datareader = cmd.ExecuteReader();
        GridView1.DataSource = sqlite_datareader;
        GridView1.DataBind();
        conn.Close();
        }

        void executeSQL(string sql)
        {
        SQLiteConnection conn = new SQLiteConnection("Data source=mydb.sqlite");
        conn.Open();
        SQLiteCommand cmd = conn.CreateCommand();
        cmd.Connection = conn;
        cmd.CommandText = sql;
        cmd.ExecuteNonQuery();
        conn.Close();
        }


        protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
        {
            //Set the edit index.
            GridView1.EditIndex = e.NewEditIndex;
            //Bind data to the GridView control.
            showdata();
        }
        protected void GridView1_OnRowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
        {
            Response.Write("cancel");
            GridView1.EditIndex = -1;
            //Bind data to the GridView control.
            showdata();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            //Update the values.
            GridViewRow row = GridView1.Rows[e.RowIndex];
            Label1.Text += ((TextBox)(row.Cells[2].Controls[0])).Text+"<br>";
            Label1.Text += ((TextBox)(row.Cells[3].Controls[0])).Text +"<br>";
            Label1.Text += ((TextBox)(row.Cells[4].Controls[0])).Text +"<br>";

            //更新的SQL語法

            string updatesql = "UPDATE address_book SET name='" + ((TextBox)(row.Cells[2].Controls[0])).Text + "', phone='" + ((TextBox)(row.Cells[3].Controls[0])).Text + "',address='" + ((TextBox)(row.Cells[4].Controls[0])).Text + "' where sid=" + ((TextBox)(row.Cells[1].Controls[0])).Text;
            executeSQL(updatesql);

            //Reset the edit index.

            GridView1.EditIndex = -1;

            //Bind data to the GridView control.
            showdata();
        }

資料庫

 

ASP.NET SQLite Sample

 //建立資料表
            SQLiteConnection conn = new SQLiteConnection("Data source=mydb.sqlite");
            //Open
            conn.Open();
            //要下任何命令先取得該連結的執行命令物件
            SQLiteCommand cmd = conn.CreateCommand();
            // 建立資料表
            string sql = "CREATE TABLE IF NOT EXISTS 'address_book' (sid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , name VARCHAR, phone VARCHAR, address VARCHAR,creation_date DATETIME DEFAULT CURRENT_TIMESTAMP)";
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            // 輸入參數
            cmd.Parameters.Add(cmd.CreateParameter());
            cmd.Parameters.Add(cmd.CreateParameter());
            cmd.Parameters.Add(cmd.CreateParameter());

            cmd.CommandText = "INSERT INTO 'address_book' ('name', 'phone', 'address' ) VALUES( ?, ?, ?)";
            cmd.Parameters[0].Value = TextBox1.Text;
            cmd.Parameters[1].Value = TextBox2.Text;
            cmd.Parameters[2].Value = TextBox3.Text;
            cmd.ExecuteNonQuery();

            // 查詢資料表
            cmd.CommandText = "SELECT * FROM address_book";
            // 執行查詢塞入 sqlite_datareader
            SQLiteDataReader sqlite_datareader = cmd.ExecuteReader();
            GridView1.DataSource = sqlite_datareader;
            GridView1.DataBind();
            conn.Close();

ASP.NET 連結 Mysql

            //建立與mysql連線
            MySqlConnection conn = new MySqlConnection("server=localhost;user id=帳號; password=密碼;database=資料庫名稱");
            conn.Open();
            MySqlCommand cmd = conn.CreateCommand();
            string sql = "CREATE TABLE IF NOT EXISTS address_book (sid INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , name VARCHAR( 20 ) NOT NULL , phone VARCHAR( 20 ) NOT NULL , address VARCHAR( 20 ) NOT NULL) ENGINE = MYISAM ;";
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();

            cmd.CommandText = "INSERT INTO address_book (name, phone, address ) VALUES('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "')";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "SELECT * FROM address_book";
            MySqlDataReader mysql_datareader = cmd.ExecuteReader();
            GridView1.DataSource = mysql_datareader;
            GridView1.DataBind();
            conn.Close();