gpt4 book ai didi

c# - SqlDataAdapter C# WCF 服务

转载 作者:行者123 更新时间:2023-11-30 17:51:34 25 4
gpt4 key购买 nike

这里是 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();
}
}

}

最佳答案

只是在你的代码中少了一行......

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

关于c# - SqlDataAdapter C# WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com