本文介绍了我如何...从对象类型system.windows.forms.textbox到已知的托管提供程序本机类型不存在映射。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 命名空间TDApplication { public partial class Contactform:Form { SqlConnection con = new SqlConnection (Data Source = .\\SQLEXPRESS; Initial Catalog = TDApplication; Integrated Security = true;); SqlCommand cmd; //更新和删除记录中使用的ID变量 int ID = 0; // int ID = @tdtext; / / int ID = tdtext.Text; public Contactform() { InitializeComponent(); DisplayData (); ClearData(); } //插入数据 private void btn_Insert_Click(object sender,EventArgs e) { if(TitalcomboBox.Text!=&& NametxtBox.Text!=) { cmd =新的SqlCommand(插入TDContact(标题,名称,H_Phone,C_Phone,W_Phone,地址,城市,州,Pin_code,P_Email,W_Email,性别) ,生日,婚姻)价值观(@ Title,@ Name,@ H_Phone,@ C_Phone,@ W_Phone,@ Address,@ City,@ State,@ Pin_code,@ P_Email,@ W_Email,@ Gender,@ Birthday,@ Marriage) ,con); con.Open(); cmd.Parameters.AddWithValue(@ Title,this.TitalcomboBox.Text.Trim()); cmd.Parameters.AddWithValue(@ Name,this。 NametxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ H_Phone,this.HPtxtBox.Text.Trim()); cmd.Parameters .AddWithValue(@ C_Phone,this.CPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ W_Phone,this.WPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ Address,this.AddrstxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ City,this。 CitytxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ State,this.StatecomboBox.Text.Trim()); cmd.Parameters .AddWithValue(@ Pin_code,this.PintxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ P_Email,this.PEmailtxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ W_Email,this.WEmailtxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ Gender,this。 GendrcomboBox.Text.Trim()); cmd.Parameters.AddWithValue(@ Birthday,this.dateTimePicker1.Value); cmd.Parameters.AddWithValue(@ Marriage,this.dateTimePicker2.Value); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show(记录成功插入); DisplayData(); ClearData(); } 其他 { MessageBox.Show(请提供详情!); } } //在Listview中显示数据 private void DisplayData() { listView1.View = View.Details; SqlDataAdapter ada = new SqlDataAdapter(选择ID,标题,名称,来自TDContact的地址,con); DataTable dt = new DataTable(); ada.Fill(dt); listView1.Items.Clear(); foreach(dt.Rows中的DataRow行) { ListViewItem item = new ListViewItem(row [ID]。ToString()); item.SubItems.Add(row [Title]。ToString()) ; item.SubItems.Add(row [Name]。ToString()); item.SubItems.Add(row [Address]。ToString( )); listView1.Items.Add(item); } } //清除数据 private void ClearData( ) { TitalcomboBox.Text =; NametxtBox.Text =; HPtxtBox。 Text =; CPtxtBox.Text =; WPtxtBox.Text =; AddrstxtBox.Text =; CitytxtBox.Text =; StatecomboBox.Text =; PintxtBox.Text =; PEmailtxtBox.Text =; WEmailtxtBox.Text =; GendrcomboBox.Text =; dateTimePicker1。 Text =; dateTimePicker2.Text =; ID = 0; } //更新记录 private void btn_Update_Click_1(对象发件人,EventArgs e) { cmd =新的SqlCommand(更新TDContact设置标题= @标题,名称= @名称,H_Phone = @ H_Phone,C_Phone = @ C_Phone,W_Phone = @ W_Phone,地址= @地址,城市= @ City,State = @ State,Pin_code = @Pin_code,P_Email = @ P_Email,W_Email = @ W_Email,Gender = @ Gender,Birthday = @ Birthday,Marriage = @ Marriage where tdtext = @id,con); con.Open(); cmd.Parameters.AddWithValue(@ id,tdtext); cmd.Parameters.AddWithValue(@ Title ,this.TitalcomboBox.Text.Trim()); cmd.Parameters.AddWithValue(@ Name,this.NametxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ H_Phone,this.HPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ C_Phone,this.CPtxtBox.Text.Trim( )); cmd.Parameters.AddWithValue(@ W_Phone,this.WPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ Address ,this.AddrstxtBox.Te xt.Trim()); cmd.Parameters.AddWithValue(@ City,this.CitytxtBox.Text.Trim()); cmd.Parameters.AddWithValue (@ State,this.StatecomboBox.Text.Trim()); cmd.Parameters.AddWithValue(@ Pin_code,this.PintxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ P_Email,this.PEmailtxtBox.Text.Trim()); cmd.Parameters.AddWithValue(@ W_Email,this.WEmailtxtBox。 Text.Trim()); cmd.Parameters.AddWithValue(@ Gender,this.GendrcomboBox.Text.Trim()); cmd.Parameters.AddWithValue (@ Birthday,this.dateTimePicker1.Value); cmd.Parameters.AddWithValue(@ Marriage,this.dateTimePicker2.Value); int k = cmd.ExecuteNonQuery(); con.Close(); if(k> 0) { MessageBox.Show(记录成功更新); DisplayData(); ClearData(); } else { MessageBox.Show(请选择要更新的记录) ); } } //选择Listview索引 私人void listView1_SelectedIndexChanged(object sender,EventArgs e) { if(listView1.SelectedItems.Count == 0) return; ListViewItem item = listView1.SelectedItems [0]; ID = Convert.ToInt32(listView1.SelectedItems [0] .SubItems [0] .Text ); TitalcomboBox.Text = listView1.SelectedItems [0] .SubItems [1] .Text; NametxtBox.Text = listView1.SelectedItems [0] .SubItems [2] .Text; //HPtxtBox.Text = listView1.SelectedItems [0] .SubItems [3] .Text; //CPtxtBox.Text = listView1.SelectedItems [0] .SubItems [4] .Text; //WPtxtBox.Text = listView1.SelectedItems [0] .SubItems [5]。文字; //AddrstxtBox.Text = listView1.SelectedItems [0] .SubItems [6] .Text; //CitytxtBox.Text = listView1.SelectedItems [0] .SubItems [7] .Text; //StatecomboBox.Text = listView1.SelectedItems [0] .SubItems [8] .Text; //PintxtBox.Text = listView1.SelectedItems [0] .SubItems [9] .Text; //PEPEtxtBox.Text = listView1.SelectedItems [0] .SubItems [10] .Text; //WEmailtxtBox.Text = listView1.SelectedItems [0] .SubItems [11] .Text; // GendrcomboBox.Text = listView1.SelectedItems [0] .SubItems [12] .Text; // dateTimePicker1。 Text = listView1.SelectedItems [0] .SubItems [13] .Text; //dateTimePicker2.Text = listView1.SelectedItems [0] .SubItems [14] .Text; } //删除记录 private void btn_Delete_Click_1(对象发件人,EventArgs e) { if(ID!= 0) { cmd = new SqlCommand(删除TDContact,其中tdtext = @ id,con); con.Open(); cmd.Parameters.AddWithValue(@ id,tdtext); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show(Record Deleted Successfully!); DisplayData(); ClearData(); } else { MessageBox.Show(请选择记录删除); } } //隐藏TextBox onclick事件 private void tdtext_TextChanged(object sender,EventArgs e) { SqlDataAdapter ada = new SqlDataAdapter(select ID ,标题,名称,H_Phone,C_Phone,W_Phone,地址,城市,州,Pin_code,P_Email,W_Email,性别,生日,婚姻来自TDContact,其中ID ='+ tdtext.Text +',con); } // private void txt_SearchName_TextChanged(object sender,EventArgs e) // { // if(txt_SearchName.Text!=){ // for(int i = listView1.Items。数 - 1; i> = 0;我 - ){ // var item = listView1.Items [i]; // if(item.Text.ToLower()。包含(txt_SearchName.Text .ToLower())){ // item.BackColor = SystemColors.Highlight; // item.ForeColor = SystemColors.HighlightText; //} // else { // listView1.Items.Remove(item); //} //} // if(listView1.SelectedItems.Count == 1){ // listView1.Focus(); //} //} //其他 // DisplayData(); //} } } 我的尝试: 不存在地图从对象类型System.Windows.Forms.TextBox到已知的托管提供者本机类型。namespace TDApplication{ public partial class Contactform : Form { SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=TDApplication;Integrated Security=true;"); SqlCommand cmd; //ID variable used in Updating and Deleting Record int ID = 0; //int ID = @tdtext; //int ID = tdtext.Text; public Contactform() { InitializeComponent(); DisplayData(); ClearData(); } //Insert Data private void btn_Insert_Click(object sender, EventArgs e) { if (TitalcomboBox.Text != "" && NametxtBox.Text != "") { cmd = new SqlCommand("insert into TDContact ( Title,Name,H_Phone,C_Phone,W_Phone,Address,City,State,Pin_code,P_Email,W_Email,Gender,Birthday,Marriage) VALUES (@Title,@Name,@H_Phone,@C_Phone,@W_Phone,@Address,@City,@State,@Pin_code,@P_Email,@W_Email,@Gender,@Birthday,@Marriage)", con); con.Open(); cmd.Parameters.AddWithValue("@Title", this.TitalcomboBox.Text.Trim()); cmd.Parameters.AddWithValue("@Name", this.NametxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@H_Phone", this.HPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@C_Phone", this.CPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@W_Phone", this.WPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@Address", this.AddrstxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@City", this.CitytxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@State", this.StatecomboBox.Text.Trim()); cmd.Parameters.AddWithValue("@Pin_code", this.PintxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@P_Email", this.PEmailtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@W_Email", this.WEmailtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@Gender", this.GendrcomboBox.Text.Trim()); cmd.Parameters.AddWithValue("@Birthday", this.dateTimePicker1.Value); cmd.Parameters.AddWithValue("@Marriage", this.dateTimePicker2.Value); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Inserted Successfully"); DisplayData(); ClearData(); } else { MessageBox.Show("Please Provide Details!"); } } //Display Data in Listview private void DisplayData() { listView1.View = View.Details; SqlDataAdapter ada = new SqlDataAdapter("select ID, Title,Name,Address from TDContact", con); DataTable dt = new DataTable(); ada.Fill(dt); listView1.Items.Clear(); foreach (DataRow row in dt.Rows) { ListViewItem item = new ListViewItem(row["ID"].ToString()); item.SubItems.Add(row["Title"].ToString()); item.SubItems.Add(row["Name"].ToString()); item.SubItems.Add(row["Address"].ToString()); listView1.Items.Add(item); } } //Clear Data private void ClearData() { TitalcomboBox.Text = ""; NametxtBox.Text = ""; HPtxtBox.Text = ""; CPtxtBox.Text = ""; WPtxtBox.Text = ""; AddrstxtBox.Text = ""; CitytxtBox.Text = ""; StatecomboBox.Text = ""; PintxtBox.Text = ""; PEmailtxtBox.Text = ""; WEmailtxtBox.Text = ""; GendrcomboBox.Text = ""; dateTimePicker1.Text = ""; dateTimePicker2.Text = ""; ID = 0; } //Update Record private void btn_Update_Click_1(object sender, EventArgs e) { cmd = new SqlCommand("update TDContact set Title=@Title,Name=@Name,H_Phone=@H_Phone,C_Phone=@C_Phone,W_Phone=@W_Phone,Address=@Address,City=@City,State=@State,Pin_code=@Pin_code,P_Email=@P_Email,W_Email=@W_Email,Gender=@Gender,Birthday=@Birthday,Marriage=@Marriage where tdtext = @id ", con); con.Open(); cmd.Parameters.AddWithValue("@id", tdtext); cmd.Parameters.AddWithValue("@Title", this.TitalcomboBox.Text.Trim()); cmd.Parameters.AddWithValue("@Name", this.NametxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@H_Phone", this.HPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@C_Phone", this.CPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@W_Phone", this.WPtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@Address", this.AddrstxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@City", this.CitytxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@State", this.StatecomboBox.Text.Trim()); cmd.Parameters.AddWithValue("@Pin_code", this.PintxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@P_Email", this.PEmailtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@W_Email", this.WEmailtxtBox.Text.Trim()); cmd.Parameters.AddWithValue("@Gender", this.GendrcomboBox.Text.Trim()); cmd.Parameters.AddWithValue("@Birthday", this.dateTimePicker1.Value); cmd.Parameters.AddWithValue("@Marriage", this.dateTimePicker2.Value); int k = cmd.ExecuteNonQuery(); con.Close(); if (k > 0) { MessageBox.Show("Record Updated Successfully"); DisplayData(); ClearData(); } else { MessageBox.Show("Please Select Record to Update"); } } // Select Listview Index private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) return; ListViewItem item = listView1.SelectedItems[0]; ID = Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text); TitalcomboBox.Text = listView1.SelectedItems[0].SubItems[1].Text; NametxtBox.Text = listView1.SelectedItems[0].SubItems[2].Text; //HPtxtBox.Text = listView1.SelectedItems[0].SubItems[3].Text; //CPtxtBox.Text = listView1.SelectedItems[0].SubItems[4].Text; //WPtxtBox.Text = listView1.SelectedItems[0].SubItems[5].Text; //AddrstxtBox.Text = listView1.SelectedItems[0].SubItems[6].Text; //CitytxtBox.Text = listView1.SelectedItems[0].SubItems[7].Text; //StatecomboBox.Text = listView1.SelectedItems[0].SubItems[8].Text; //PintxtBox.Text = listView1.SelectedItems[0].SubItems[9].Text; //PEmailtxtBox.Text = listView1.SelectedItems[0].SubItems[10].Text; //WEmailtxtBox.Text = listView1.SelectedItems[0].SubItems[11].Text; //GendrcomboBox.Text = listView1.SelectedItems[0].SubItems[12].Text; //dateTimePicker1.Text = listView1.SelectedItems[0].SubItems[13].Text; //dateTimePicker2.Text = listView1.SelectedItems[0].SubItems[14].Text; } //Delete Record private void btn_Delete_Click_1(object sender, EventArgs e) { if (ID != 0) { cmd = new SqlCommand("delete TDContact where tdtext=@id", con); con.Open(); cmd.Parameters.AddWithValue("@id", tdtext); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Deleted Successfully!"); DisplayData(); ClearData(); } else { MessageBox.Show("Please Select Record to Delete"); } } // hidden TextBox onclick event private void tdtext_TextChanged(object sender, EventArgs e) { SqlDataAdapter ada = new SqlDataAdapter("select ID, Title,Name,H_Phone,C_Phone,W_Phone,Address,City,State,Pin_code,P_Email,W_Email,Gender,Birthday,Marriage from TDContact Where ID = '"+ tdtext.Text+"' ", con); } //private void txt_SearchName_TextChanged(object sender, EventArgs e) //{ // if (txt_SearchName.Text != "") { //for(int i = listView1.Items.Count - 1; i >= 0; i--) { // var item = listView1.Items[i]; // if (item.Text.ToLower().Contains(txt_SearchName.Text.ToLower())) { // item.BackColor = SystemColors.Highlight; // item.ForeColor = SystemColors.HighlightText; // } // else { // listView1.Items.Remove(item); // } // } // if (listView1.SelectedItems.Count == 1) { // listView1.Focus(); // } // } // else // DisplayData(); // } } }What I have tried:No mapping exists from object type System.Windows.Forms.TextBox to a known managed provider native type.推荐答案因为tdtext是一个文本框:Because tdtext is a textbox:SqlDataAdapter ada = new SqlDataAdapter("select ID, Title,Name,H_Phone,C_Phone,W_Phone,Address,City,State,Pin_code,P_Email,W_Email,Gender,Birthday,Marriage from TDContact Where ID = '"+ tdtext.Text+"' ", con);你不能把它传递给SQL:you can't just pass it to SQL:cmd.Parameters.AddWithValue("@id", tdtext); 您需要传递其Text属性:You need to pass its Text property:cmd.Parameters.AddWithValue("@id", tdtext.Text); 并删除字符串连接!无论你多久使用它都很危险!And remove the string concatenation! That is dangerous regardless of how infrequently you use it! 这篇关于我如何...从对象类型system.windows.forms.textbox到已知的托管提供程序本机类型不存在映射。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 02:52