您的位置:首页 > 数据库

数据库连接、读取(查询)以及赋值给变量过程

2011-12-02 15:15 393 查看
1.数据库连接、读取以及赋值给变量 
            int userId = Convert.ToInt32(Session["UserId"]);

                string strconn = System.Configuration.ConfigurationManager.AppSettings["Main.ConnectionString"].Trim() + " ; Provider=SQLOLEDB;";

                OleDbConnection cnn = new OleDbConnection(strconn);

                string sql = "select Tag1,Tag2,Tag3,Tag4,Media,Options 
from JackDonkey_User  where UserId=" + userId;

                try

                {

                    cnn.Open();

                    OleDbCommand cmd = new OleDbCommand(sql, cnn);

                    OleDbDataReader read = cmd.ExecuteReader();

                    if (read.Read())

                    {

                        string Tag1 = read["Tag1"].ToString().Trim();

                        string Tag2 = read["Tag2"].ToString().Trim();

                        string Tag3 = read["Tag3"].ToString().Trim();

                        string Tag4 = read["Tag4"].ToString().Trim();

                        string Media = read["Media"].ToString().Trim();

                        string Options = read["Options"].ToString().Trim();

                    }
                    read.Close();

                    cnn.Close();

                }

                catch (Exception ex)

                {

                    if (cnn.State == ConnectionState.Open)

                    {

                        cnn.Close();

                    }

                }

 

2.数据库连接、查询以及赋值给变量

 string strconn = System.Configuration.ConfigurationManager.AppSettings["Main.ConnectionString"].Trim() + " ; Provider=SQLOLEDB;";

        OleDbConnection cnn = new OleDbConnection(strconn);

        string sql = "select TagId from JackDonkey_Tag  where TagName= '" +txt_tag1.Text.Trim()+"'";

        cnn.Open();

        OleDbCommand cmd = new OleDbCommand(sql, cnn);

        object obj = cmd.ExecuteScalar();   

        string tag1 =Convert.ToString(obj);


cnn.Close();

 

2。 string strconn = System.Configuration.ConfigurationManager.AppSettings["Main.ConnectionString"].Trim() + " ; Provider=SQLOLEDB;";

        OleDbConnection cnn = new OleDbConnection(strconn);

        string sql;

        #region --Pictures--

        if (Media == "Pictures")

        {

            sql = "select top 13 * from JackDonkey_Picture where ImgId in (select ImgId from JackDonkey_TagImgThou where TagId=" + Tag + ")" + " and RowStatus='active'";

            if (Options == "Most Recent")

            {

                sql = sql + " " + "order by CreatedDate DESC";

            }

            if (Options == "Most Active")

            {

                sql = sql + " " + "order by YesCount DESC";

            }

            if (Options == "Time Frame Drop down")

            {

            }

            string ImgId = "0";

            string Name = "";

            string ImgUrl = "null.jpg";

            string UName = "";

            //string DSC = "";

            try

            {

                cnn.Open();

                OleDbCommand cmd = new OleDbCommand(sql, cnn);

                OleDbDataReader read = cmd.ExecuteReader();

                int rowCount = 1;

                if (read.Read())

                {

                    do

                    {

                        Name = read["ImgName"].ToString().Trim();                

                        ImgId = read["ImgId"].ToString().Trim();//=Pic_Id is not correct 

                        ImgUrl = read["ImgUrl"].ToString().Trim();                     

                        UName = read["Username"].ToString().Trim();

                        if (rowCount == 1)

                        {

                            largeImgSource1.ImageUrl = ImgUrl;//bigpic

                            largeImgSourceLink1.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            largeImgSourceLink1.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 2)

                        {

                            imgPicture11.ImageUrl = ImgUrl;

                            imgPictureLink11.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink11.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 3)

                        {

                            imgPicture12.ImageUrl = ImgUrl;

                            imgPictureLink12.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink12.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 4)

                        {

                            imgPicture13.ImageUrl = ImgUrl;

                            imgPictureLink13.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink13.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 5)

                        {

                            imgPicture14.ImageUrl = ImgUrl;

                            imgPictureLink14.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink14.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 6)

                        {

                            imgPicture21.ImageUrl = ImgUrl;

                            imgPictureLink21.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink21.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 7)

                        {

                            imgPicture22.ImageUrl = ImgUrl;

                            imgPictureLink22.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink22.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 8)

                        {

                            imgPicture23.ImageUrl = ImgUrl;

                            imgPictureLink23.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink23.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 9)

                        {

                            imgPicture24.ImageUrl = ImgUrl;

                            imgPictureLink24.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink24.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 10)

                        {

                            imgPicture31.ImageUrl = ImgUrl;

                            imgPictureLink31.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink31.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 11)

                        {

                            imgPicture32.ImageUrl = ImgUrl;

                            imgPictureLink32.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink32.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 12)

                        {

                            imgPicture41.ImageUrl = ImgUrl;

                            imgPictureLink41.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink41.Title = Name + ", thanks to " + UName;

                        }

                        if (rowCount == 13)

                        {

                            imgPicture42.ImageUrl = ImgUrl;

                            imgPictureLink42.HRef = "JackDonkeyGame.aspx?p=" + UrlEncrypt.Encrypt(ImgId.ToString());

                            imgPictureLink42.Title = Name + ", thanks to " + UName;

                        }

                        rowCount++;

                    } while (read.Read());

                }

                read.Close();

                cnn.Close();

            }

            catch (Exception ex)

            {

                if (cnn.State == ConnectionState.Open)

                {

                    cnn.Close();

                }

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐