C#/ Access - 无法绑定到DataSource上的属性或列名称。参数名称:dataMember

时间:2013-02-27 10:17:02

标签: c# database

我正在尝试使用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();

1 个答案:

答案 0 :(得分:0)

我认为它应该是这样的: -

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