SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";
String Strt_Address = TextBox1.Text;
String End_Address = TextBox2.Text;
String filePath = FileUpload1.PostedFile.FileName;
String filename = Path.GetFileName(filePath);
String ExtStr = Path.GetExtension(filename);
String contenttype = String.Empty;
switch (ExtStr)
{
case ".png":
contenttype = "image/png";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".gif":
contenttype = "image/gif";
break;
}
if (contenttype != string.Empty)
{
Stream Strmf = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(Strmf);
Byte[] imgbytes = br.ReadBytes((Int32)Strmf.Length);
//SqlDataReader sqldread = new SqlDataReader();
String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "';";
{
SqlCommand scmd = new SqlCommand(selQuery, conn);
conn.Open();
SqlDataReader sqldread = scmd.ExecuteReader();
while (sqldread.Read())
{
int Dbid = (int)sqldread["Id"];
//string DbId = sqldread.GetInt32("Id").ToString();
Label4.Text = Convert.ToString(Dbid);
String QueryStr = "INSERT INTO User_Images(Id,Image) VALUES ('" + Dbid + "',@Image)";
SqlCommand scmd1 = new SqlCommand(QueryStr, conn);
scmd1.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
scmd1.ExecuteNonQuery();
}
//String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";
//SqlCommand scmd = new SqlCommand(QueryStr, conn);
//scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
sqldread.Close();
conn.Close();
}
}
首先,如果条件将转换为我通过文件上传控件上传文件的字节。
Select 命令将检查用户文本框输入名称是否等于数据库值。如果相等,代码将从该表中获取 id 并将其存储为 int 变量。
我上传的图像,该 id 将转到另一个表。
我的第一个 SQL 表 =
Id = int
Source = varchar(max)
我的第二个 SQL 数据表 =
image_id = int (primary key) Auto increment
Id = int (first table id store in here)
image = (varbinary)MAX
场景是……例如 Facebook,如果 Facebook 帐户名有效,我们可以上传无限的照片。所以这里的情况是一样的。如果我的名字与数据库相同,它将检索其 ID,并在该 ID 下将图像插入到第二个数据表
当我想检索这些图像时。我可以使用 for 循环从第二个表中检索图像.. 是吗?