[英]SqlDataAdapter C# WCF Service
Total C# beginner here. 总C#初学者在这里。 I'm trying to update a "Patient" table in an SQL database from a Web Form.
我正在尝试从Web窗体更新SQL数据库中的“Patient”表。 I'm calling a "PatientRegistration" method of a WCF service I've written to do so.
我正在调用我编写的WCF服务的“PatientRegistration”方法。 When a Patient is added, the service returns "True", if it fails, it returns "False".
添加患者时,服务返回“True”,如果失败,则返回“False”。
The app builds, runs and returns "true"... but when I check the database, none of the Patients I've added appear in the table (even after a refresh). 应用程序构建,运行并返回“true”...但是当我检查数据库时,我添加的患者都没有出现在表中(即使刷新后)。
Can someone spot where I might be going wrong? 有人可以找到我可能出错的地方吗? Here is my code for the "database service":
这是我的“数据库服务”代码:
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();
}
}
}
Just a missing a line in your code... 只是在你的代码中缺少一行......
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.