文档库 最新最全的文档下载
当前位置:文档库 › 验证码@二进制存储图片@listview批量提交

验证码@二进制存储图片@listview批量提交

注册(验证码)

private void label4_Click(object sender, EventArgs e)
{
codes = CreateCode(4);
CreateImg(codes);
}

private void Regedit_Load(object sender, EventArgs e)
{
this.toolStripStatusLabel2.Text = System.DateTime.Now.ToString("D");
codes = CreateCode(4);
CreateImg(codes);
}
private string CreateCode(int length)
{
Random ran = new Random();
string codeStr = string.Empty;
switch (types)
{
case "数字":
for (int i = 0; i < length; i++)
{
codeStr += ((char)ran.Next(48, 57)).ToString();
}
CreateImg(codeStr);
return codeStr;
default:
return null;
}

}
private void CreateImg(string code)
{
Bitmap bmp = null;
if (types == "数字")
{
bmp = new Bitmap((int)Math.Ceiling(code.Length * 9.5) + 10, 22);
}
//字符像素大小不一样 在此创建长度不同的bmp
Graphics g = Graphics.FromImage(bmp);

try
{
//生成随机生成器

Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int i = 0; i < 5; i++)
{
int x1 = random.Next(bmp.Width);
int x2 = random.Next(bmp.Width);
int y1 = random.Next(bmp.Height);
int y2 = random.Next(bmp.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, bmp.Width, bmp.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(code, font, brush, 3, 2);
//画图片的前景干扰点
for (int i = 0; i < 5; i++)
{
int x = random.Next(bmp.Width);
int y = random.Next(bmp.Height);
bmp.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);
label4.Width = bmp.Width;
label4.Height = bmp.Height;
label4.Image = bmp;
}
catch
{
}
finally
{
g.Dispose();
}

}
public object[

] CreateRegionCode(int strlength)
{
//定义一个字符串数组储存汉字编码的组成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

Random rnd = new Random();

//定义一个object数组用来
object[] bytes = new object[strlength];

/**/
/*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
每个汉字有四个区位码组成
区位码第1位和区位码第2位作为字节数组第一个元素
区位码第3位和区位码第4位作为字节数组第二个元素
*/
for (int i = 0; i < strlength; i++)
{
//区位码第1位
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();

//区位码第2位
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();

//区位码第3位
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();

//区位码第4位
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();

}
return bytes;
}



二进制存储图片

public void SaveImage(int MID, OpenFileDialog openF)//将图片以二进制存入数据库中
{
string strimg = openF.FileName.ToString(); //记录图片的所在路径
try
{
FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
conn.Open();
StringBuilder strSql = new StringBuilder();
strSql.Append("update emoIformation Set epimage=@Photo where epID=" + MID);
SqlCommand cmd = new SqlCommand(strSql.T

oString(), conn);
cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
cmd.ExecuteNonQuery();
}
catch (SqlException)
{

throw;
}
conn.Close();
}
public void Get_Image(int id, PictureBox pb)//将图片从数据库中取出
{


byte[] imagebytes = null;
conn.Open();
string sql = string.Format("select * from emoIformation where epID={0}", id);
SqlCommand com = new SqlCommand(sql, conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(6);
}
dr.Close();
conn.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
pb.Image = bmpt;
}



listview批量提交到(数据库)

private void button3_Click(object sender, EventArgs e)
{
string[] sql = new string[listView1.Items.Count];
int index = 0;
//productgoodsinfo p = new productgoodsinfo();
for (int i = 0; i < listView1.Items.Count; i++)
{
sql[index] = string.Format("insert into productgoods values({0},'{1}',{2},{3},'{4}')", int.Parse(listView1.Items[i].SubItems[0].Text), listView1.Items[i].SubItems[1].Text, int.Parse(listView1.Items[i].SubItems[2].Text), double.Parse(listView1.Items[i].SubItems[3].Text), listView1.Items[i].SubItems[4].Text);
index++;
}
if (db.ExecuteNone(sql))
{
MessageBox.Show("提交成功,请等待审批!","提示信息",MessageBoxButtons.OK,https://www.wendangku.net/doc/e816234055.html,rmation);
this.listView1.Items.Clear();
}
else
{
MessageBox.Show("失败", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

事物方法:
public bool ExecuteNone(string[] sql)
{
bool result;
conn.Open();
//事物开始
SqlTransaction tran = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//指定Cmd的事物
cmd.Transaction = tran;

try
{
//循环执行SQL语句
for (int i = 0; i < sql.Length; i++)
{
if (sql[i] == null || sql[i] == "")
{
continue;
}
https://www.wendangku.net/doc/e816234055.html,mandText = sql[i];
cmd.ExecuteNonQuery();
}
//事物提交
https://www.wendangku.net/doc/e816234055.html,mit()

;
result = true;
}

catch (System.Exception)
{
//事物回滚
tran.Rollback();
result = false;
}
finally
{
conn.Close();
}


return result;

}

相关文档
相关文档 最新文档