简体   繁体   English

如何使用 C# 在 SQL 服务器数据库中保存 DataGridViewComboBoxColumn

[英]How save DataGridViewComboBoxColumn in SQL Server database using C#

I am going to develop school management system and want to save attendance record in database from DataGridViewComboBoxColumn each student in class with Save button.我将开发学校管理系统,并希望使用保存按钮将 class 中每个学生的出勤记录保存在数据库中。

I means set all student record eg present, absent or leave the save record in database when the "save" button is pressed.我的意思是当按下“保存”按钮时设置所有学生记录,例如存在、缺席或将保存记录留在数据库中。

This is my code - it works when I change cell of 1st student这是我的代码 - 当我更改第一个学生的单元格时它可以工作

private void AttendanceDataGridVies_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        AttendanceDataGridVies.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }

    private void AttendanceDataGridVies_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        string comboboxSelectedValue = string.Empty;

        if (AttendanceDataGridVies.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewComboBoxColumn))
        {
            comboboxSelectedValue = AttendanceDataGridVies.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            label2.Text = comboboxSelectedValue;
        }
    }

You probably want to set up something like Entity Framework to handle the database functions.您可能想要设置类似 Entity Framework 的东西来处理数据库功能。 This article is several years old now, but I think it can probably get you on your way: Entity-Framework-Tutorial-for-Beginners这篇文章已经有好几年了,但我认为它可能会让你上路: Entity-Framework-Tutorial-for-Beginners

Also, typically, you would want to separate the DB layer from your presentation layer.此外,通常,您希望将 DB 层与表示层分开。 However, you can set up your own connection strings and write your own SQL instead, if you would like.但是,如果您愿意,您可以设置自己的连接字符串并编写自己的 SQL。 To do so, you basically need to:为此,您基本上需要:

  1. Setup a connection string设置连接字符串
  2. Create a SQL statement创建 SQL 语句
  3. Open SQL connection打开SQL连接
  4. Call SQL command调用 SQL 命令
  5. Close the connection关闭连接

A very simple example connecting to a local SQL MDF inside the project:一个非常简单的示例连接到项目内的本地 SQL MDF:

       private void button1_Click(object sender, EventArgs e)
        {
            addToDatabase();
        }

        private void addToDatabase()
        {
            string connection = @"Server=(LocalDB)\MSSQLLocalDB;attachdbfilename=C:\Users\xxxxx\source\repos\MySolution\MyProject\MyDatabase.mdf;integrated security=True;";
            SqlConnection sqlConnection = new SqlConnection(connection);

            string query = "INSERT INTO Table1 " +
                "(Id, Column1, Column2) " +
                "VALUES (@Id,  @Column1, @Column2) ";
            SqlCommand cmd = new SqlCommand(query, sqlConnection);
            cmd.Parameters.Add("@Id", SqlDbType.Int).Value = 1;
            cmd.Parameters.Add("@Column1", SqlDbType.VarChar, 50).Value = "value1";
            cmd.Parameters.Add("@Column2", SqlDbType.VarChar, 50).Value = "Value2";

            try
            {
                sqlConnection.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sqlConnection.Close();
            }
        }

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

相关问题 如何在c#中将字典保存和访问到SQL Server数据库中 - how to save and access Dictionary into SQL Server Database in c# 如何在C#中的SQL Server数据库中保存CheckBox值 - How to save CheckBox value in SQL Server database in C# 如何在C#中将JSON数据保存到SQL Server数据库? - How to Save JSON data to SQL server database in C#? 如何在C#中从数据库显示DataGridViewComboBoxColumn中的值 - How to display value in DataGridViewComboBoxColumn from database in C# 如何使用C#将BiFir数据保存到sql数据库? - How to save this biFir data to sql database using c#? 如何使用C#代码从ftp服务器文件夹中自动拾取文件并将文件名内容保存在SQL数据库表中 - How to auto pick up files from ftp server folder and save the filename content in SQL database table using C# code C#使用函数绑定DataGridViewComboBoxColumn数据源 - c# bind DataGridViewComboBoxColumn DataSource using a function 如何使用C#将复选框值存储到SQL Server数据库中 - How to store a checkbox value into sql server database using c# 如何使用C#搜索SQL Server数据库 - How to search SQL Server database using C# Winforms:如何使用C#将图片上传到SQL Server数据库 - Winforms: How to upload a picture into SQL Server database using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM