简体   繁体   English

通过C#插入MySQL

[英]INSERT INTO MySQL through C#

I am making a WPF C# exc. 我正在制作WPF C#演示。 I have a DAO class, with connection to my Database and also Service class, with some methods for getting information from Database. 我有一个DAO类,它与我的数据库以及Service类相关联,并且具有一些从数据库中获取信息的方法。 It works fine. 工作正常。 But i want to insert to database also. 但是我也想插入数据库。 So, where is my mistake? 那么,我的错误在哪里? I have method in Service class with that code 我在服务类中使用该代码的方法

public static DataTable createProject(string projectName, string depName, string empName, int estTime, DateTime startDate)
{
   string sql = "";
   sql += "INSERT INTO Projects (projectName, departmentName, employeeName, estimatedTime, startDate)";
   sql += "VALUES (" + projectName + depName + empName + estTime + startDate +")";

   return getDataTable(sql);
}

And after that, i am going to my xaml.cs 之后,我要去我的xaml.cs

private void btnCreateAdd_Click(object sender, RoutedEventArgs e)
{
   Service.createProject((string)txtProjName.Text, (string)cmbCreateDepartment.SelectedItem, (string)cmbCreateEmployees.SelectedItem, Int32.Parse(txtElapseTime.Text), (DateTime)Calendar.SelectedDate);
}

It gives me some exeption in my xaml.cs 它给了我xaml.cs一些启发

Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'. 无法将类型为“ System.Data.DataRowView”的对象转换为类型为“ System.String”的对象。

Look at the type of cmbCreateDepartment.SelectedItem and cmbCreateEmployees.SelectedItem attributes. 查看cmbCreateDepartment.SelectedItemcmbCreateEmployees.SelectedItem属性的类型。 It's a System.Data.DataRowView and not a String ! 这是System.Data.DataRowView而不是String so the exception is logic. 因此例外是逻辑。

Try this 尝试这个

private void btnCreateAdd_Click(object sender, RoutedEventArgs e)
{
Service.createProject((string)txtProjName.Text, ((ComboBoxItem)cmbCreateDepartment.SelectedItem).Content.ToString(), ((ComboBoxItem)cmbCreateEmployees.SelectedItem).Content.ToString(), Int32.Parse(txtElapseTime.Text), (DateTime)Calendar.SelectedDate);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM