如何使用本地数据库在C#中创建数据集

时间:2018-04-16 17:20:44

标签: c# sql database winforms local-database

我是C#的初学者并在Windows窗体应用程序中为客户数据创建了一个数据库,它的功能是获取用户的电话号码,然后搜索表格然后找到它然后填充数据字段有了这些信息;否则需要将新客户添加到表中。

当我运行代码时出现错误

  

System.ArgumentException:'不支持关键字:'数据源(localdb)\ mssqlocaldb; attachdbfilename'。'

这是我的代码:

    DataTable TableCust;
    SqlConnection cnCust;
    SqlDataAdapter dataCust;
    DataGrid dtGridCust;
    public bool buttonClicked = false;
    private static int CurrentOrder = 1000;
    private int i = 0;
    Validation v = new Validation();

    public frmPizzaPetes()
    {
        InitializeComponent();
    }
    string dataSource;
    string SqlParms;
    private void Form1_Load(object sender, EventArgs e)
    {
        btnAccept.Enabled = false;
        lblOrderNo.Text = CurrentOrder.ToString();
        Price();
        //
        dataSource = @"Data Source(LocalDB)\MSSQLocalDB;AttachDbFilename=|C:\Users\tyada\DATABASE\Pizza.mdf;";
        SqlParms = "Integrated Securtiy=True; Connect Timeout==30";
        string SqlCust = "select * from Customers";
        string strConn = dataSource + SqlParms;
        cnCust = new SqlConnection(strConn);
        cnCust.Open();
        TableCust = new DataTable();
        dtGridCust.DataSource = TableCust;
    }

 public bool ifCustIsFound()
    {
        bool tester=false;
        string SqlCustomer = "SELECT*FROM Customers WHERE CustPhone= '" + mtbPhone.Text + "';";
        dataCust = new SqlDataAdapter(SqlCustomer, cnCust);
        dataCust.Fill(TableCust);

        if (TableCust.Rows.Count > 0)
        {
            txtName.Text = TableCust.Rows[0]["CustName"].ToString();
            txtAddress.Text = TableCust.Rows[0]["CustAddress"].ToString();
            txtApt.Text = TableCust.Rows[0]["CustSuite"].ToString();
            txtCity.Text = TableCust.Rows[0]["CustCity"].ToString();
            mtbZip.Text = TableCust.Rows[0]["CustZip"].ToString();
            cboState.Text = TableCust.Rows[0]["CustState"].ToString();
           // dtGridCust.DataSource = TableCust;
        }


        else
        {
            DialogResult dlg=MessageBox.Show("Add Customer?","Customer not found", MessageBoxButtons.YesNo);
            if (dlg == DialogResult.Yes)
            {
                     string strConn = dataSource + SqlParms;
                     SqlDataAdapter adaptSQL = new SqlDataAdapter(strSQL, strConn);
                     SqlCommand cmdCust = new SqlCommand();
                     SqlCommandBuilder cmdBld = new SqlCommandBuilder(adaptSQL);
                     DataRow newCust;
                     newCust = TableCust.NewRow();   
                     newCust["CustPhone"] = mtbPhone.Text;
                     newCust["CustName"] = txtName.Text;
                     newCust["CustAddress1"] = txtAddress.Text;
                     newCust["CustAddress2"] = txtApt.Text;
                     newCust["CustCity"] = txtCity.Text;
                     newCust["CustState"] = cboState.Text;
                     newCust["CustZip"] = mtbZip.Text;
                     try
                     {
                         TableCust.Rows.Add(newCust);  
                         cmdBld.GetUpdateCommand();   
                         adaptSQL.Update(TableCust);
                         MessageBox.Show("Customer Added!");
                     }
                     catch (SqlException)
                     {
                         MessageBox.Show("Customer Add Failed!");
                     }

            } 
            txtName.Focus();

        }

        return tester;
    }

1 个答案:

答案 0 :(得分:0)

数据源中似乎缺少“=”。试试这个。

 private void Form1_Load(object sender, EventArgs e)
    {
        btnAccept.Enabled = false;
        lblOrderNo.Text = CurrentOrder.ToString();
        Price();
        //
        dataSource = @"Data Source =(LocalDB)\MSSQLocalDB;AttachDbFilename=|C:\Users\tyada\DATABASE\Pizza.mdf;";
        SqlParms = "Integrated Securtiy=True; Connect Timeout==30";
        string SqlCust = "select * from Customers";
        string strConn = dataSource + SqlParms;
        cnCust = new SqlConnection(strConn);
        cnCust.Open();
        TableCust = new DataTable();
        dtGridCust.DataSource = TableCust;
    }