Winform中使用PictureBox显示及修改数据库中的照片

    做了程序,需要管理人员信息,其中就有照片。一般来说这种blob字段的信息比较难搞定,后来发现在winform上放置picturebox并与bindingsource进行绑定后几乎都不用写代码可以轻松实现照片信息的修改和显示。不过实现的过程中走了不少弯路。

    picturebox的属性设置:(databindings)中的Image属性设置为人员的BindingSource中的相应照片字段。

    BindingSource对象所绑定的TableAdapt中的update和insert的command设置要注意,其中照片字段的参数属性providertype设置为longvarbinary或varbinary,长度为0.

    最要注意的问题是:不要使用access来向mdb的照片字段插入数据,否则无法显示。用自己的程序实现照片的插入。

    唯一需要编写代码的地方是picturebox控件的click事件,实现用户通过点击该控件可以把照片文件加载到picturebox控件,剩下的都交给vs自动完成(照片显示、修改到数据库等):

            OpenFileDialog openfile  =   new  OpenFileDialog();
            String FileName 
=   "" ;
            
if  (openfile.ShowDialog()  ==  DialogResult.OK)
            {
                FileName 
=  openfile.FileName;
                
this .photo.Image  =  Image.FromFile(FileName);
            }

    因为使用了bindingnavigator等绑定控件,很多代码都省了,呵呵。

您可以使用ADO.NET或Entity Framework等数据访问技术,将WinForm应用程序的添加商品操作与数据库进行交互,将商品信息保存到数据库。具体操作流程如下: 1. 创建数据库表,用于存储商品信息。 2. 在WinForm应用程序,添加添加商品的功能,即在界面上提供商品信息的输入框和添加按钮等控件。 3. 在添加按钮的事件处理方法,获取用户输入的商品信息,并通过数据库连接对象创建SqlCommand对象,将商品信息插入到数据库。 4. 在WinForm应用程序添加查询商品的功能,即在界面上提供查询商品信息的输入框和查询按钮等控件。 5. 在查询按钮的事件处理方法,获取用户输入的商品信息,使用SqlCommand对象查询数据库的商品信息,并将查询结果显示在界面上。 以下是C#代码示例: ``` // 添加商品 private void btnAdd_Click(object sender, EventArgs e) { // 获取用户输入的商品信息 string name = txtName.Text; decimal price = decimal.Parse(txtPrice.Text); int inventory = int.Parse(txtInventory.Text); // 将商品信息插入到数据库 string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; using(SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand("INSERT INTO Product(Name, Price, Inventory) VALUES(@Name, @Price, @Inventory)", connection); command.Parameters.AddWithValue("@Name", name); command.Parameters.AddWithValue("@Price", price); command.Parameters.AddWithValue("@Inventory", inventory); command.ExecuteNonQuery(); } } // 查询商品 private void btnSearch_Click(object sender, EventArgs e) { // 获取用户输入的商品名称 string name = txtSearchName.Text; // 查询数据库的商品信息 string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; using(SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand("SELECT * FROM Product WHERE Name=@Name", connection); command.Parameters.AddWithValue("@Name", name); SqlDataReader reader = command.ExecuteReader(); while(reader.Read()) { // 将查询结果显示在界面上 string productName = reader.GetString(1); decimal productPrice = reader.GetDecimal(2); int productInventory = reader.GetInt32(3); // TODO: 在界面上显示商品信息 } } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值