首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

我们可以在c# winform中插入一些数据,同时插入值选择查询吗

在C# WinForm中,可以使用ADO.NET来连接和操作数据库。你可以使用SQL语句将数据插入到数据库中,并且可以在插入数据后立即执行查询操作。

首先,你需要在C# WinForm项目中添加对数据库的连接。可以使用System.Data.SqlClient命名空间来连接SQL Server数据库,或者使用其他适配器来连接其他类型的数据库。

以下是一个示例代码,展示如何在C# WinForm中插入数据并执行查询操作:

代码语言:txt
复制
// 引入所需命名空间
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WinFormApp
{
    public partial class Form1 : Form
    {
        // 数据库连接字符串
        string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=YourDatabase;Integrated Security=True";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            // 获取要插入的数据
            string name = txtName.Text;
            int age = Convert.ToInt32(txtAge.Text);

            try
            {
                // 创建连接对象
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 创建插入数据的SQL语句
                    string insertQuery = "INSERT INTO YourTable (Name, Age) VALUES (@Name, @Age)";

                    // 创建命令对象
                    using (SqlCommand command = new SqlCommand(insertQuery, connection))
                    {
                        // 添加参数并设置值
                        command.Parameters.AddWithValue("@Name", name);
                        command.Parameters.AddWithValue("@Age", age);

                        // 执行插入操作
                        command.ExecuteNonQuery();
                    }
                }

                MessageBox.Show("数据插入成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据插入失败:" + ex.Message);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建连接对象
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 创建查询数据的SQL语句
                    string query = "SELECT * FROM YourTable";

                    // 创建适配器对象
                    using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
                    {
                        // 创建数据集
                        DataSet dataSet = new DataSet();

                        // 填充数据集
                        adapter.Fill(dataSet);

                        // 将数据集中的数据显示在DataGridView控件中
                        dataGridView1.DataSource = dataSet.Tables[0];
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询数据失败:" + ex.Message);
            }
        }
    }
}

在上述示例代码中,btnInsert_Click事件处理程序会将txtName和txtAge文本框中的数据插入到数据库表中。btnQuery_Click事件处理程序会执行查询操作并将结果显示在DataGridView控件中。你可以根据自己的实际需求修改和扩展代码。

注意:这里提供的是一个示例,你需要根据自己的实际情况修改连接字符串、数据库表名、字段等信息。

推荐的腾讯云产品:腾讯云数据库SQL Server版。该产品提供高性能、高可用的云数据库服务,适用于WinForm项目中的数据库存储需求。详情请参考:腾讯云数据库SQL Server版

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券