在C#中,你可以使用ADO.NET来连接和操作SQL数据库。以下是一个示例代码,演示如何将sop_name文本框和sop_image图片框中的值保存到SQL的sop表中对应的字段:\n\ncsharp\nusing System;\nusing System.Data.SqlClient;\nusing System.Drawing;\nusing System.IO;\nusing System.Windows.Forms;\n\nnamespace YourNamespace\n{\n public partial class YourForm : Form\n {\n private string connectionString = "your_connection_string_here"; // 替换为你的数据库连接字符串\n\n public YourForm()\n {\n InitializeComponent();\n }\n\n private void btnSave_Click(object sender, EventArgs e)\n {\n string sopName = txtSopName.Text;\n Image sopImage = picSopImage.Image;\n\n // 将图片转换为字节数组\n byte[] imageData = null;\n if (sopImage != null)\n {\n using (MemoryStream ms = new MemoryStream())\n {\n sopImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);\n imageData = ms.ToArray();\n }\n }\n\n // 使用ADO.NET连接数据库并插入数据\n using (SqlConnection connection = new SqlConnection(connectionString))\n {\n connection.Open();\n\n string query = "INSERT INTO sop (sop_name, sop_image) VALUES (@SopName, @SopImage)";\n SqlCommand command = new SqlCommand(query, connection);\n\n command.Parameters.AddWithValue("@SopName", sopName);\n command.Parameters.AddWithValue("@SopImage", imageData);\n\n command.ExecuteNonQuery();\n }\n\n MessageBox.Show("保存成功!");\n }\n }\n}\n\n\n在上述代码中,假设你已经在窗体设计中创建了名为txtSopName的文本框和名为picSopImage的图片框,并且在保存按钮的点击事件处理程序btnSave_Click中执行保存操作。\n\n请注意替换代码中的your_connection_string_here为你的实际数据库连接字符串。此外,如果你的图片是其他格式(如PNG),你需要相应地更改ImageFormat.Jpeg为适当的格式。

C# 将文本框和图片框数据保存到 SQL 数据库 -  SOP 表操作示例

原文地址: https://www.cveoy.top/t/topic/p9eP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录