数据库连接、读取(查询)以及赋值给变量过程
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();
}
}
}
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();
}
}
}
相关文章推荐
- SQL Server知识点:数据库之局部变量的定义、赋值、以及使用
- 如何用eclipse将数据库oracle数据相连接,实现查询数据库中的信息以及更改添加表中内容
- 如何读取配置文件并连接后台数据库,且对数据库进行操作实例(查询或修改等)
- 通过数据库读取,获取MVM各扫描任务的漏扫数量趋势统计以及详细信息查询
- 数据库的连接查询及带变量的查询
- Mysql存储过程查询结果赋值到变量的方法
- 两张图解读Oracle 数据库的 union all、union、intersect、minus以及各种连接查询。
- JDBC实现增删改查、模糊查询、分页查询、子查询以及体现单例设计模式连接数据库
- 利用javabean轻松实现对数据库的连接、查询以及增删改
- ADO.NET 用windows方式连接数据库以及简单的增,删,改,查询操作
- mysql存储过程判断不为空和不为空,查询结果赋值变量
- 数据库技术_Orcale技术(0002)_5分钟会用存储过程_存储过程简单实例(包含循环、条件、增改查、参数传入、变量赋值、java调用等)
- Mysql_知识总结03(级联操作+多表查询【左外连接查询*重点掌握】+数据库的存储过程)
- Mysql存储过程查询结果赋值到变量的方法
- 连接数据库的步骤SQL Server 与mysql 以及一些数据库查询语句
- Mysql 存储过程查询结果赋值到变量的方法
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- Mysql存储过程查询结果赋值到变量的方法
- Servlet变量范围使用指南以及数据库连接
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程