简体   繁体   English

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

[英]How do I create a dataset in C# with local database

I am a beginner in C# and created a database in a Windows Forms App for a customer data, the function of it is to take the user's phone number and then search the table and then if it is found then populate the data fields with the information; 我是C#的初学者,并在Windows Forms App中为客户数据创建了一个数据库,它的功能是获取用户的电话号码,然后搜索表格,如果找到该表格,则使用信息填充数据字段; otherwise it needs to add the new customer to the table. 否则,需要将新客户添加到表中。

When I run the code I get an error 运行代码时出现错误

System.ArgumentException: 'Keyword not supported: 'data source(localdb)\\mssqlocaldb;attachdbfilename'.' System.ArgumentException:'不支持的关键字:'数据源(localdb)\\ mssqlocaldb; attachdbfilename'。

This is my code: 这是我的代码:

    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;
    }

it seems "=" is missing in data source. 似乎数据源中缺少“ =”。 Try this. 尝试这个。

 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;
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM