3

总 C# 初学者在这里。我正在尝试从 Web 表单更新 SQL 数据库中的“患者”表。我正在调用我为此编写的 WCF 服务的“PatientRegistration”方法。添加 Patient 时,服务返回“True”,如果失败,则返回“False”。

该应用程序构建、运行并返回“true”......但是当我检查数据库时,我添加的患者都没有出现在表中(即使在刷新之后)。

有人可以发现我可能出错的地方吗?这是我的“数据库服务”代码:

namespace ADOWebApp2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ADODatabaseService" in code, svc and config file together.
    public class ADODatabaseService : IADODatabaseService
    {
        public bool PatientRegistration(string hno, string fname, string lname, int pnum, string address, string email)
        {
            string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\xxxxx\\Documents\\Visual Studio 2010\\Projects\\ADOWebApp2\\ADOWebApp2\\App_Data\\ADOdb.mdf;Integrated Security=True;User Instance=True";
            SqlConnection conn = new SqlConnection(connString);
            string sqlquery = "select * from Patient";
            SqlDataAdapter sqladapter = new SqlDataAdapter();
            SqlCommandBuilder cb = new SqlCommandBuilder(sqladapter);
            try
            {
                conn.Open();
                sqladapter.SelectCommand = new SqlCommand(sqlquery, conn);
                DataSet patient = new DataSet();
                sqladapter.Fill(patient, "Patient");
                DataRow row = patient.Tables["Patient"].NewRow();
                row[0] = hno;
                row[1] = fname;
                row[2] = lname;
                row[3] = pnum;
                row[4] = address;
                row[5] = email;
                sqladapter.Update(patient, "Patient");
                return true;
            }

            catch (Exception)
            {
                return false;
            }

            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }

        }
4

1 回答 1

1

只是代码中缺少一行...

 DataRow row = patient.Tables["Patient"].NewRow();
 row[0] = hno;
 row[1] = fname;
 row[2] = lname;
 row[3] = pnum;
 row[4] = address;
 row[5] = email;
 patient.Tables["Patient"].Rows.Add(row);  // <- Add the row to the collection
 sqladapter.Update(patient, "Patient");
于 2013-10-08T14:43:09.927 回答