コード例 #1
0
ファイル: PTTDATA.cs プロジェクト: Altria09137/PTTHelper
        public List <PTTDATAtype> GetPTTDATA()
        {
            List <PTTDATAtype> cards = new List <PTTDATAtype>();

            string        Constr        = @"Data Source=WIN-K602VN7RVVF\SQLEXPRESS;Initial Catalog=PTT_Helper;Persist Security Info=True;User ID=sa;Password=Miku01";
            SqlConnection sqlConnection = new SqlConnection(Constr);
            SqlCommand    sqlCommand    = new SqlCommand("select * from dbo.PTTDATA ");

            sqlCommand.Connection = sqlConnection;
            sqlConnection.Open();


            SqlDataReader reader = sqlCommand.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    PTTDATAtype card = new PTTDATAtype
                    {
                        ID     = reader.GetInt64(reader.GetOrdinal("id")),
                        pop    = reader.GetString(reader.GetOrdinal("pop")),
                        title  = reader.GetString(reader.GetOrdinal("title")),
                        author = reader.GetString(reader.GetOrdinal("author")),
                    };
                    cards.Add(card);
                }
            }
            else
            {
                Console.WriteLine("資料庫為空!");
            }
            sqlConnection.Close();
            return(cards);
        }
コード例 #2
0
        public List <PTTDATAtype> PTTcontextSearch(int id)
        {
            List <PTTDATAtype> cards = new List <PTTDATAtype>();

            string        Constr        = @"Data Source=WIN-K602VN7RVVF\SQLEXPRESS;Initial Catalog=PTT_Helper;Persist Security Info=True;User ID=sa;Password=Miku01";
            SqlConnection sqlConnection = new SqlConnection(Constr);
            SqlCommand    sqlCommand    = new SqlCommand("select * from dbo.PTTDATA where id=@id");

            sqlCommand.Connection = sqlConnection;
            sqlCommand.Parameters.Add(new SqlParameter("@id", id));
            SqlDataAdapter dat = new SqlDataAdapter();

            dat.SelectCommand = sqlCommand;
            dat.Fill(dt);
            dat.Dispose();
            sqlConnection.Open();



            try
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PTTDATAtype card = new PTTDATAtype
                    {
                        ID = Convert.ToInt32(dt.Rows[i]["ID"]),

                        context = Convert.ToString(dt.Rows[i]["context"]),
                    };
                    cards.Add(card);
                }
                ;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            return(cards);
        }