gpt4 book ai didi

c# - 将图像写入 SQL Server CE

转载 作者:行者123 更新时间:2023-11-30 22:16:59 25 4
gpt4 key购买 nike

enter image description here

我在表单应用程序上有一个按钮,它应该将图像写入已使用 OpenFileDialog 选择的 SQL Server 数据库。

我想要完成的是定制此代码以将所选图像写入我设置的 SQL Server Compact Edition,数据库称为 TestImage.sdf 并且表称为测试表。目前,当我点击商店按钮时,我得到了

Cannot locate SQL server error

我的代码:

private void updatedata() 
{
//use filestream object to read the image.
//read to the full length of image to a byte array.
//add this byte as an oracle parameter and insert it into database.
try
{
//proceed only when the image has a valid path
if (imagename != "")
{
FileStream fs;
fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
//a byte array to read the image
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
//open the database using odp.net and insert the data
string connstr = @"Data Source=.;Initial Catalog=TestImage;
Persist Security Info=True;User ID=sa";

SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string query;
query = "insert into test_table(id_image,pic) values(" +
textBox1.Text + "," + " @pic)";
SqlParameter picparameter = new SqlParameter();
picparameter.SqlDbType = SqlDbType.Image;
picparameter.ParameterName = "pic";
picparameter.Value = picbyte;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(picparameter);
cmd.ExecuteNonQuery();
MessageBox.Show("Image Added");
cmd.Dispose();
conn.Close();
conn.Dispose();
Connection();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

最佳答案

SQL Server Compact Edition 需要它自己的一组 ADO.NET 类才能运行。

不能使用SqlConnectionSqlCommand等用于SQL Server的类,但应该使用相应的SqlCeConnection SqlCeCommand 等...

using  System.Data.SqlServerCe;

private void updatedata()
{
try
{
if (imagename != "")
{
FileStream fs;
byte[] picbyte;
using(fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read))
{
//a byte array to read the image
picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
}
string connstr = @"Data Source=.\TestImage.sdf;Persist Security Info=False;"
string query = "insert into test_table(id_image,pic) " +
"values(@id, @pic)";
using(SqlCeConnection conn = new SqlCeConnection(connstr))
using(SqlCeCommand cmd = new SqlCeCommand(query, conn))
{
conn.Open();
SqlCeParameter picparameter = new SqlCeParameter();
picparameter.SqlDbType = SqlDbType.Image;
picparameter.ParameterName = "pic";
picparameter.Value = picbyte;
cmd.Parameters.Add(picparameter);
SqlCeParameter idparameter = new SqlCeParameter();
idparameter.SqlDbType = SqlDbType.Int;
idparameter.ParameterName = "id";
idparameter.Value = textBox1.Text;
cmd.Parameters.Add(idparameter);
cmd.ExecuteNonQuery();
MessageBox.Show("Image Added");
}
}
}
.....

我已经修改了连接字符串以添加您要使用的数据库的文件名(假设它在您的应用程序的同一目录中)并适应了 SQL Server CE 使用的标准文本。

然后我添加了适当的using statement以确保正确处理连接和命令。

作为最后一件事,我还为 id_image 字段添加了参数(请注意,如果此字段是身份列,则不应尝试在此处插入任何内容)

关于c# - 将图像写入 SQL Server CE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17014277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com