图片存储到SQL Server

            byte[] imageBytes = GetImageBytes(pictureBox1.Image);

            string connStr = "SQL Server连接字符串";

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sql = "Insert Into T_Img Values (@ImgData) ";
                using (SqlCommand cmd = new SqlCommand(sql))
                {
                    SqlParameter param = new SqlParameter("ImgData", SqlDbType.VarBinary, imageBytes.Length);
                    param.Value = imageBytes;
                    cmd.Parameters.Add(param);

                    cmd.Connection = conn;
                    conn.Open();
                    int i = cmd.ExecuteNonQuery();
                    MessageBox.Show(i.ToString());
                }
            }
posted @ 2012-06-30 23:14  liqipeng  阅读(154)  评论(0编辑  收藏  举报