我正在构建一个项目,在该项目中我访问数据库表,将行和列插入和更新到表中。
我正在使用 更新数据库中的表,GridView每次都使用它向另一个表添加新列。
我在 button_Click 上的代码是:
com = new SqlCommand("ALTER TABLE Location_Profile_Master ADD " + LocProName.Text + " int", con);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
            foreach (GridViewRow row in GridView1.Rows)
            {
                string Pro_Name = row.Cells[1].Text;
                for (int i = 1; i < GridView1.Columns.Count; i++)
                {
                    int n = Convert.ToInt16(row.Cells[i + 1].Text);
                    //short n = 0;
                    //if (Int16.TryParse(row.Cells[i + 1].Text, out n))
                    //{
                        com = new SqlCommand("UPDATE Location_Profile_Master SET " + LocProName.Text + "='" + n + "' WHERE Profile_Name='" + Pro_Name.ToString().Trim() + "' ", con);
                        con.Open();
                        com.ExecuteNonQuery();
                        con.Close();
                    //}
                }
            }
因此,在添加列及其值时,我想检查该列名是否已经存在于数据库中,如果不存在,我想检查列值是否已经存在于数据库中。应通知用户该列已存在于数据库中。
另外,当我插入一行时,我想知道该行是否已经存在于表中。
请帮助。