简体   繁体   English

添加图像作为方法的参数

[英]Adding image as a parameter of a method

I have a C# winform application with a database. 我有一个带有数据库的C#winform应用程序。 In the database I have one DataTable called Airlines. 在数据库中,我有一个名为Airlines的数据表。 The columns of this DataTable are "Name", "Country", "Telephone", "Email", "Logo". 该数据表的列为“名称”,“国家”,“电话”,“电子邮件”,“徽标”。

I want to create a method to be used in Windows Form application. 我想创建一种在Windows窗体应用程序中使用的方法。 This method is going to be 这种方法将是

public int AddNewAirline (string name, string country, string telephone, string email, xxxxx)

The parameters will be taken from Windows Form text boxes, and the logo by the Browse... option. 参数将从Windows窗体文本框中获取,徽标从“浏览...”选项获取。 I have created the method and the SQL operation is: 我已经创建了该方法,SQL操作为:

"INSERT INTO Airlines VALUES('"+name+"','....blah blah blah.... ¿but what about the logo? )";

So my main question, is: What parameter should I add on the xxxxxx part of the method header, and what the SQL operation should be like? 所以我的主要问题是:我应该在方法标题的xxxxxx部分添加什么参数,以及SQL操作应该是什么样的? I'm a bit messed with this and I couldn't find a solution on the Internet. 我对此有些困惑,无法在Internet上找到解决方案。

In order to store the path to the image in the database, you can do it the same way you are saving the other parameters: 为了将图像的路径存储在数据库中,可以使用与保存其他参数相同的方式进行操作:

public int AddNewAirline (string name, string country, string telephone, string email, string logoPath)

You SQL insert statement is missing the column names. 您的SQL插入语句缺少列名称。 Try this: 尝试这个:

"INSERT INTO Airlines ('Name', 'Country', 'Telephone', 'Email', 'Logo') " +
"VALUES ('" + name + "', '" + country + "', '" + telephone + "', '" + email + "', '" + logoPath + "')"

You should be aware that in taking this approach, the possibility exists that the image could be moved or deleted, leaving your database with a path to a file that no longer exists. 您应该意识到,采用这种方法时,可能会移动或删除图像,从而使数据库中的文件路径不再存在。 In order to resolve this, you could make a copy of the image to a safe location and reference the copy in your database, or you could store the actual bytes of the image in your database as a BLOB. 为了解决此问题,您可以将映像的副本复制到安全位置,然后在数据库中引用该副本,也可以将映像的实际字节存储为BLOB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM