c# / Access -不能绑定到数据源上的属性或列名.参数名称:dataMember

本文关键字:属性 参数 dataMember Access 不能 绑定 数据源 | 更新日期: 2023-09-27 18:01:26

我正在尝试使用c#和Access数据库创建一个程序,并通过创建每次添加的新行将其连接和添加值到表中,现在我遇到了这个问题,我找不到适合我的程序的解决方案。这是我的部分代码

        public string connstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:'College'Year 2'Structured Programming'Go Greece Ver.1'Database'GoGreece.accdb;Persist Security Info=False;";
        private void savebutton_Click(object sender, EventArgs e)
        {
            {
                OleDbConnection hotelConn = new OleDbConnection(connstr);
                InitializeComponent(); 
                OleDbCommand hotelComm = new OleDbCommand();
                OleDbDataAdapter hotelAdapter = new OleDbDataAdapter();
                DataTable hotelData = new DataTable();
                DataSet ds = new DataSet();
                hotelConn.Open();
                hotelComm.Connection = hotelConn;
                hotelComm.CommandText = " INSERT INTO Hotels (Name) VALUES ('" + nametext + "')";
                int temp = hotelComm.ExecuteNonQuery();
                if (temp > 0)
                {
                    nametext.Text = null;
                    MessageBox.Show("Successfully added into database");
                }
                else
                {
                    MessageBox.Show("Records were not added into the database");
                }
                hotelConn.Close();

c# / Access -不能绑定到数据源上的属性或列名.参数名称:dataMember

我觉得应该是这样的:-

string connstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:'College'Year 2'Structured Programming'Go Greece Ver.1'Database'GoGreece.accdb;Persist Security Info=False;";

using (OleDbConnection con = new OleDbConnection(connstr))
        {
            string SQlstring = string.Format("INSERT INTO [Hotels] (Name) VALUES ('{0}')", nametext);
            OleDbCommand cmd = new OleDbCommand(SQlstring, con);
            try
            {
                con.Open();
                int result = cmd.ExecuteNonQuery();
            }
            catch(OleDbException Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }