📄 dataaccess.cs
字号:
namespace SMSTest
{
using System;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// DataAccess 的摘要说明。
/// </summary>
public class DataAccess
{
private System.Data.SqlClient.SqlCommand selectCommand;
private System.Data.SqlClient.SqlCommand insertCommand;
private System.Data.SqlClient.SqlDataAdapter dataAdapter;
private System.Data.SqlClient.SqlConnection connection;
private SMSTest.CustomerDataSet customerData;
public DataAccess()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
this.selectCommand = new System.Data.SqlClient.SqlCommand();
this.insertCommand = new System.Data.SqlClient.SqlCommand();
this.connection = new System.Data.SqlClient.SqlConnection();
this.customerData = new SMSTest.CustomerDataSet();
//
// dataAdapter
//
this.dataAdapter.InsertCommand = this.insertCommand;
this.dataAdapter.SelectCommand = this.selectCommand;
this.dataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
new System.Data.Common.DataColumnMapping("Name", "Name"),
new System.Data.Common.DataColumnMapping("Phone", "Phone")})});
//
// selectCommand
//
this.selectCommand.CommandText = "SELECT Phone FROM Customers WHERE (Name = @Name)";
this.selectCommand.Connection = this.connection;
this.selectCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, "Name"));
//
// insertCommand
//
this.insertCommand.CommandText = "INSERT INTO Customers(Name, Phone) VALUES (@Name, @Phone)";
this.insertCommand.Connection = this.connection;
this.insertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, "Name"));
this.insertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.VarChar, 15, "Phone"));
//
// connection
//
this.connection.ConnectionString = "workstation id=\"XZTHINK-NB\";packet size=4096;user id=sa;data source=\".\";persist s" +
"ecurity info=True;initial catalog=TestSMS";
//
// customerData
//
this.customerData.DataSetName = "CustomerDataSet";
this.customerData.Locale = new System.Globalization.CultureInfo("zh-CN");
}
//添加
public void Insert(CustomerDataSet customer)
{
this.dataAdapter.Update(customer);
}
//根据姓名获得号码
public CustomerDataSet GetCustomerByName(string name)
{
customerData.Clear();
this.selectCommand.Parameters["@Name"].Value = name;
this.dataAdapter.Fill(this.customerData,CustomerDataSet.CUSTOMERS_TABLE);
return customerData;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -