主题:[讨论]请教各位高手一个C#从数据库取数据问题!详见文章描述!希望有高手抽空能解答下!谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
namespace ImageadminProject.ImageManage
{
public partial class ImageOutput : Form
{
FileStream fs;
//定义一个数据集对象
private DataSet MyDataSet = new DataSet();
//定义查询使用的字符串
private string MySql = "Select ImageNo AS 图像编号,Imagename AS 图像名称,BelongproNo AS 所属项目工程,"
+ "Imageheight AS 图像高度,Imagewidth AS 图像宽度,ROIHeight AS ROI高度,ROIWidth AS ROI宽度,ROIX AS ROI起点X坐标,ROIY AS ROI起点Y坐标,"
+ "GPSlongitude AS GPS经度,GPSlatitude AS GPS纬度,GPSaltitude AS GPS海拔,GPSdatetime AS GPS时间,Imageposition AS 图像位置,"
+ "Imageassess AS 图像评定,Memory AS 图像备注,Isgivereport AS 是否已出报告,IsRecCD AS 是否已刻光盘 From ImageInfo";
//定义数据表名称
private string MyTable = "ImageInfo";
private string mytable = "ImagePhotos";
//定义一个数据适配器对象
private SqlDataAdapter MySqlDataAdapter;
//定义数据连接对象
private SqlConnection connection;
//定义命令绑定对象
private SqlCommandBuilder MySqlCommandBulider;
//定义一个公用方法所在的对象
ImageadminProject.PublicFunction MyFunction = new ImageadminProject.PublicFunction();
public ImageOutput()
{
InitializeComponent();
}
private void ImageOutput_Load(object sender, EventArgs e)
{
//获得连接对象
connection = MyFunction.GetSqlConnection();
//获得数据适配器对象
MySqlDataAdapter = MyFunction.GetSqlDataAdapter(MySql);
//给数据适配器对象添加表映射
MySqlDataAdapter.TableMappings.Add("Table", MyTable);
//设置数据适配器对象的查询语句
MySqlDataAdapter.SelectCommand = new SqlCommand(MySql, connection);
//绑定数据适配器对象
MySqlCommandBulider = new SqlCommandBuilder(MySqlDataAdapter);
//填充数据集
MySqlDataAdapter.Fill(MyDataSet);
//赋予数据绑定对象的数据源属性
this.bindingSource1.DataSource = this.MyDataSet;
//赋予数据绑定对象的数据成员属性
this.bindingSource1.DataMember = MyTable;
//绑定显示和编辑数据的各种对象的数据源属性
this.dataGridView1.DataSource = this.bindingSource1;
this.textBoxImageNo.DataBindings.Add("Text", this.bindingSource1, "图像编号");
this.textBoxImagename.DataBindings.Add("Text", this.bindingSource1, "图像名称");
this.textBoxBelongproNo.DataBindings.Add("Text", this.bindingSource1, "所属项目工程");
this.textBoxImageheight.DataBindings.Add("Text", this.bindingSource1, "图像高度");
this.textBoxImagewidth.DataBindings.Add("Text", this.bindingSource1, "图像宽度");
this.textBoxROIHeight.DataBindings.Add("Text", this.bindingSource1, "ROI高度");
this.textBoxROIWidth.DataBindings.Add("Text", this.bindingSource1, "ROI宽度");
this.textBoxROIX.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxROIY.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxGPSlongitude.DataBindings.Add("Text", this.bindingSource1, "GPS经度");
this.textBoxGPSlatitude.DataBindings.Add("Text", this.bindingSource1, "GPS纬度");
this.textBoxGPSaltitude.DataBindings.Add("Text", this.bindingSource1, "GPS海拔");
this.dateTimePicker1.DataBindings.Add("Text", this.bindingSource1, "GPS时间");
this.textBoxImageSaveas.DataBindings.Add("Text", this.bindingSource1, "图像位置");
this.textBoxImageassess.DataBindings.Add("Text", this.bindingSource1, "图像评定");
this.textBoxMemory.DataBindings.Add("Text", this.bindingSource1, "图像备注");
this.comboBoxIsgivereport.DataBindings.Add("Text", this.bindingSource1, "是否已出报告");
this.comboBoxIsRecCD.DataBindings.Add("Text", this.bindingSource1, "是否已刻光盘");
this.DisPlayNumber();
}
private void buttonDatabasetoPictureBox_Click(object sender, EventArgs e)
{
string theFile;
//定义一个保存文件的对话框
SaveFileDialog MySaveFileDialog = new SaveFileDialog();
MySaveFileDialog.InitialDirectory = Application.ExecutablePath;
//定义保存文件的类型
MySaveFileDialog.Filter = "JPeg Image(*.jpg)|*.jpg|Bitmap Image(*.bmp)|*.bmp|Gif Image(*.gif)|*.gif";
MySaveFileDialog.Title = "影像图像另存为";
//显示保存文本框
MySaveFileDialog.ShowDialog();
theFile = MySaveFileDialog.FileName;
this.pictureBoxImagephoto.ImageLocation = this.textBoxImageSaveas.Text;
try
{
if (theFile != "")
{
connection = MyFunction.GetSqlConnection();
SqlDataAdapter da = new SqlDataAdapter("Select * From ImagePhotos", connection);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("ImagePhotos");
byte[] MyData = new byte[0];
da.Fill(ds, "ImagePhotos");
DataRow myRow;
myRow = ds.Tables["ImagePhotos"].Rows[0]; //书上的实例也是这样从数据表中取数据的,但是我使用此句不论选择那一条数据,取出来的始终是数据表中的第一行!而不是点击到dataGridView对应的行,能导出对应行的图片数据!若改为下面的for循环,始终取出来的都是数据表中的最后一行!请各位高手抽空解答一下啊!谢谢!
【
for (int i = 0; i < ds.Tables["ImagePhotos"].Rows.Count; i++)
{
myRow = ds.Tables["ImagePhotos"].Rows[i];
if (myRow["Imagepicture"].ToString() != " ")
{
MyData = (byte[])myRow["Imagepicture"];
}
}
】
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream(@theFile, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close();
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
public class Win32
{
[DllImport("shell32.dll", EntryPoint = "ShellExecuteA")]
public static extern int ShellExecute(
int hwnd,
String lpOperation,
String lpFile,
String lpParameters,
String lpDirectory,
int nShowCmd
);
}
private void buttonDistanceRead_Click(object sender, EventArgs e)
{
Win32.ShellExecute(0, String.Empty, "mailto:xxxxx@msn.com", String.Empty, String.Empty, 1);
}
//单击“首记录”按钮的执行代码
private void buttonMoveFirst_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
//单击“下一条”按钮的执行代码
private void buttonMoveNext_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position + 1 < this.bindingSource1.Count)
{
this.bindingSource1.MoveNext();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“上一条”按钮的执行代码
private void buttonMovePrevious_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position > 0)
{
this.bindingSource1.MovePrevious();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“尾记录”按钮的执行代码
private void buttonMoveLast_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
//显示记录数、总数
private void DisPlayNumber()
{
this.labelPosition.Text = "当前记录:" + (this.bindingSource1.Position + 1).ToString();
this.labelCount.Text = "总记录数:" + this.bindingSource1.Count.ToString();
}
//单击数据网格控件的记录时更新记录数和总数的显示
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.DisPlayNumber();
}
private void buttonExit_Click(object sender, EventArgs e)
{
//释放窗体
this.Close();
//释放资源
this.Dispose();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
namespace ImageadminProject.ImageManage
{
public partial class ImageOutput : Form
{
FileStream fs;
//定义一个数据集对象
private DataSet MyDataSet = new DataSet();
//定义查询使用的字符串
private string MySql = "Select ImageNo AS 图像编号,Imagename AS 图像名称,BelongproNo AS 所属项目工程,"
+ "Imageheight AS 图像高度,Imagewidth AS 图像宽度,ROIHeight AS ROI高度,ROIWidth AS ROI宽度,ROIX AS ROI起点X坐标,ROIY AS ROI起点Y坐标,"
+ "GPSlongitude AS GPS经度,GPSlatitude AS GPS纬度,GPSaltitude AS GPS海拔,GPSdatetime AS GPS时间,Imageposition AS 图像位置,"
+ "Imageassess AS 图像评定,Memory AS 图像备注,Isgivereport AS 是否已出报告,IsRecCD AS 是否已刻光盘 From ImageInfo";
//定义数据表名称
private string MyTable = "ImageInfo";
private string mytable = "ImagePhotos";
//定义一个数据适配器对象
private SqlDataAdapter MySqlDataAdapter;
//定义数据连接对象
private SqlConnection connection;
//定义命令绑定对象
private SqlCommandBuilder MySqlCommandBulider;
//定义一个公用方法所在的对象
ImageadminProject.PublicFunction MyFunction = new ImageadminProject.PublicFunction();
public ImageOutput()
{
InitializeComponent();
}
private void ImageOutput_Load(object sender, EventArgs e)
{
//获得连接对象
connection = MyFunction.GetSqlConnection();
//获得数据适配器对象
MySqlDataAdapter = MyFunction.GetSqlDataAdapter(MySql);
//给数据适配器对象添加表映射
MySqlDataAdapter.TableMappings.Add("Table", MyTable);
//设置数据适配器对象的查询语句
MySqlDataAdapter.SelectCommand = new SqlCommand(MySql, connection);
//绑定数据适配器对象
MySqlCommandBulider = new SqlCommandBuilder(MySqlDataAdapter);
//填充数据集
MySqlDataAdapter.Fill(MyDataSet);
//赋予数据绑定对象的数据源属性
this.bindingSource1.DataSource = this.MyDataSet;
//赋予数据绑定对象的数据成员属性
this.bindingSource1.DataMember = MyTable;
//绑定显示和编辑数据的各种对象的数据源属性
this.dataGridView1.DataSource = this.bindingSource1;
this.textBoxImageNo.DataBindings.Add("Text", this.bindingSource1, "图像编号");
this.textBoxImagename.DataBindings.Add("Text", this.bindingSource1, "图像名称");
this.textBoxBelongproNo.DataBindings.Add("Text", this.bindingSource1, "所属项目工程");
this.textBoxImageheight.DataBindings.Add("Text", this.bindingSource1, "图像高度");
this.textBoxImagewidth.DataBindings.Add("Text", this.bindingSource1, "图像宽度");
this.textBoxROIHeight.DataBindings.Add("Text", this.bindingSource1, "ROI高度");
this.textBoxROIWidth.DataBindings.Add("Text", this.bindingSource1, "ROI宽度");
this.textBoxROIX.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxROIY.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxGPSlongitude.DataBindings.Add("Text", this.bindingSource1, "GPS经度");
this.textBoxGPSlatitude.DataBindings.Add("Text", this.bindingSource1, "GPS纬度");
this.textBoxGPSaltitude.DataBindings.Add("Text", this.bindingSource1, "GPS海拔");
this.dateTimePicker1.DataBindings.Add("Text", this.bindingSource1, "GPS时间");
this.textBoxImageSaveas.DataBindings.Add("Text", this.bindingSource1, "图像位置");
this.textBoxImageassess.DataBindings.Add("Text", this.bindingSource1, "图像评定");
this.textBoxMemory.DataBindings.Add("Text", this.bindingSource1, "图像备注");
this.comboBoxIsgivereport.DataBindings.Add("Text", this.bindingSource1, "是否已出报告");
this.comboBoxIsRecCD.DataBindings.Add("Text", this.bindingSource1, "是否已刻光盘");
this.DisPlayNumber();
}
private void buttonDatabasetoPictureBox_Click(object sender, EventArgs e)
{
string theFile;
//定义一个保存文件的对话框
SaveFileDialog MySaveFileDialog = new SaveFileDialog();
MySaveFileDialog.InitialDirectory = Application.ExecutablePath;
//定义保存文件的类型
MySaveFileDialog.Filter = "JPeg Image(*.jpg)|*.jpg|Bitmap Image(*.bmp)|*.bmp|Gif Image(*.gif)|*.gif";
MySaveFileDialog.Title = "影像图像另存为";
//显示保存文本框
MySaveFileDialog.ShowDialog();
theFile = MySaveFileDialog.FileName;
this.pictureBoxImagephoto.ImageLocation = this.textBoxImageSaveas.Text;
try
{
if (theFile != "")
{
connection = MyFunction.GetSqlConnection();
SqlDataAdapter da = new SqlDataAdapter("Select * From ImagePhotos", connection);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("ImagePhotos");
byte[] MyData = new byte[0];
da.Fill(ds, "ImagePhotos");
DataRow myRow;
myRow = ds.Tables["ImagePhotos"].Rows[0]; //书上的实例也是这样从数据表中取数据的,但是我使用此句不论选择那一条数据,取出来的始终是数据表中的第一行!而不是点击到dataGridView对应的行,能导出对应行的图片数据!若改为下面的for循环,始终取出来的都是数据表中的最后一行!请各位高手抽空解答一下啊!谢谢!
【
for (int i = 0; i < ds.Tables["ImagePhotos"].Rows.Count; i++)
{
myRow = ds.Tables["ImagePhotos"].Rows[i];
if (myRow["Imagepicture"].ToString() != " ")
{
MyData = (byte[])myRow["Imagepicture"];
}
}
】
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream(@theFile, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close();
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
public class Win32
{
[DllImport("shell32.dll", EntryPoint = "ShellExecuteA")]
public static extern int ShellExecute(
int hwnd,
String lpOperation,
String lpFile,
String lpParameters,
String lpDirectory,
int nShowCmd
);
}
private void buttonDistanceRead_Click(object sender, EventArgs e)
{
Win32.ShellExecute(0, String.Empty, "mailto:xxxxx@msn.com", String.Empty, String.Empty, 1);
}
//单击“首记录”按钮的执行代码
private void buttonMoveFirst_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
//单击“下一条”按钮的执行代码
private void buttonMoveNext_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position + 1 < this.bindingSource1.Count)
{
this.bindingSource1.MoveNext();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“上一条”按钮的执行代码
private void buttonMovePrevious_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position > 0)
{
this.bindingSource1.MovePrevious();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“尾记录”按钮的执行代码
private void buttonMoveLast_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
//显示记录数、总数
private void DisPlayNumber()
{
this.labelPosition.Text = "当前记录:" + (this.bindingSource1.Position + 1).ToString();
this.labelCount.Text = "总记录数:" + this.bindingSource1.Count.ToString();
}
//单击数据网格控件的记录时更新记录数和总数的显示
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.DisPlayNumber();
}
private void buttonExit_Click(object sender, EventArgs e)
{
//释放窗体
this.Close();
//释放资源
this.Dispose();
}
}
}