formadapter.cs
来自「csharp课本的源代码」· CS 代码 · 共 53 行
CS
53 行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SqlDataAdapterExample
{
public partial class FormAdapter : Form
{
SqlDataAdapter adapter;
DataTable table;
public FormAdapter()
{
InitializeComponent();
}
private void buttonOpen_Click(object sender, EventArgs e)
{
string tableName = (radioButtonMyTable1.Checked == true ? "MyTable1" : "MyTable2");
string connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
adapter = new SqlDataAdapter("select * from " + tableName, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.InsertCommand = builder.GetInsertCommand();
adapter.DeleteCommand = builder.GetDeleteCommand();
adapter.UpdateCommand = builder.GetUpdateCommand();
table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
}
private void buttonSave_Click(object sender, EventArgs e)
{
dataGridView1.EndEdit();
try
{
adapter.Update(table);
MessageBox.Show("保存成功!");
}
catch (SqlException err)
{
MessageBox.Show(err.Message, "保存失败!");
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?