这是我的第一个 Windows 窗体/SQL 应用程序。如何在提交按钮按下时更新对数据库的表更改?
尝试添加“dt”数据表时,它无法识别它,但我不知道如何将按钮功能放在 try/catch 语句中。
显然它们现在有两个功能,但是我如何使它作为一个功能工作呢?
代码如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
namespace AccountsApp
{
public partial class Search : Form
{
public Search(string searchString, string searchType)
{
InitializeComponent();
try
{
if (searchString != null)
{
string strCon = "Data Source=OMIW2310.orthman.local;Initial Catalog=Accts;Integrated Security=True";
string strSQL = "SELECT dbo.Account.UserName, dbo.Account.Password, dbo.Account.URL, dbo.Host.HostName, dbo.Service.ServiceName, dbo.ServiceType.SvcTypeName FROM dbo.Account INNER JOIN dbo.Host ON dbo.Account.AccountHost = dbo.Host.HostID INNER JOIN dbo.Service ON dbo.Account.AccountService = dbo.Service.ServiceID INNER JOIN dbo.ServiceType ON dbo.Account.AccountType = dbo.ServiceType.SvcTypeID WHERE " + searchType + "= '" + searchString + "'";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandbuilder = new SqlCommandBuilder(dataAdapter);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
dbGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dbGridView.DataSource = dt;
textBox1.Text = searchString;
textBox2.Text = searchType;
}
} catch (SqlException ex){
MessageBox.Show(ex.Message);
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Search_Load(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void CancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
dbGridView.Update();
}
}
}