使用C#插入数据库 [英] insert into the database using c#

查看:106
本文介绍了使用C#插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在为学生制作一个简单的注册表.
为此,我编写了一个名为"sp_stuDetails"的存储过程,其代码如下


hi all,

i m making a simple registration form of the student.
for this i writ a stored procedure named as"sp_stuDetails" whose code is as follows


USE [C:\DOCUMENTS AND SETTINGS\HP-ZEZ\MY DOCUMENTS\VISUAL STUDIO 2008\PROJECTS\GETSETGO_CCE\GETSETGO_CCE\CCE.MDF]
GO



CREATE proc [dbo].[sp_stuDetails]
@strSRNo varchar(25),
@datAdmission datetime,
@strName varchar(50),
@strGender varchar(1),
@datBirth datetime,
@strFather varchar(50),
@strMother varchar(50),
@strGaurdian varchar(200),
@strAddress varchar(200),
@strPermanenetAddress varchar(200),
@strContactDetails varchar(200),
@strMobile1 varchar(25),
@strMobile2 varchar(25),
@intCityID int,
@intStateID int,
@intDistrictID int,
@intReturn int=0 output
as
begin
declare @intNextID int

begin transaction
 select @intNextID = isnull(max(intStudentID),0)+1 from tblStudent1;
 insert into tblStudent1 (
intStudentID,
strSRNo,
datAdmission,
strName,
strGender,
datBirth,
strFather,
strMother,
strGaurdian,
strAddress,
strPermanenetAddress,
strContactDetails,
strMobile1,
strMobile2,
intCityID,
intStateID,
intDistrictID
)
values(
@intNextID,
@strSRNo,
@datAdmission,
@strName,
@strGender,
@datBirth,
@strFather,
@strMother,
@strGaurdian,
@strAddress,
@strPermanenetAddress,
@strContactDetails,
@strMobile1,
@strMobile2,
@intCityID,
@intStateID,
@intDistrictID
)
if @@error <> 0
    begin
        rollback transaction
        return @intReturn
    end
    commit transaction
    set @intReturn = @intNextID
    return @intReturn
end


GO



并在.cs文件的保存按钮单击事件中,我编写了如下代码...



and in .cs file Save button click event i write the code which is as follows...

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["cbsegrade"].ToString());
       

            SqlCommand cmd = new SqlCommand("sp_stuDetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            cmd.Parameters.AddWithValue("@strSRNo", txtSRNo.Text);
            cmd.Parameters.AddWithValue("@datAdmission", txtdatAdmission);
            cmd.Parameters.AddWithValue("@strName", txtStudentName);
            cmd.Parameters.AddWithValue("@strGender", ddlGender.SelectedValue);
            cmd.Parameters.AddWithValue("@datBirth", txtDob);
            cmd.Parameters.AddWithValue("@strFather", txtFatherName.Text);
            cmd.Parameters.AddWithValue("@strMother", txtMotherName.Text);
            cmd.Parameters.AddWithValue("@strGaurdian", txtGaurdian.Text);
            cmd.Parameters.AddWithValue("@strAddress", txtCurAdd.Text);
            cmd.Parameters.AddWithValue("@strPermanenetAddress", txtPerAdd.Text);
            cmd.Parameters.AddWithValue("@strContactDetails", txtConDetails.Text);
            cmd.Parameters.AddWithValue("@strMobile1", txtMob1.Text);
            cmd.Parameters.AddWithValue("@strMobile2", txtMob2.Text);
            cmd.Parameters.AddWithValue("@intCityID", ddlCity.SelectedValue);
            cmd.Parameters.AddWithValue("@intStateID", ddlState.SelectedValue);
            cmd.Parameters.AddWithValue("@intDistrictID", ddlDistrict.SelectedValue);
            cmd.ExecuteNonQuery();
            con.Close();




但是当我运行Web表单时
它给出了以下错误


从对象类型System.Web.UI.WebControls.TextBox到已知的托管提供程序本机类型不存在映射.


请帮我.

谢谢和问候
subiya




but when i run the web form
it gives the following error


No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.


plz help me out.

thanks and regard
subiya

推荐答案

我添加了 .txtDob
.Text 属性.
cmd.Parameters.AddWithValue("@datBirth", txtDob.Text);


这行可能是问题-cmd.Parameters.AddWithValue("@datAdmission", txtdatAdmission);
确保在此字段中传递有效的日期时间格式.
This line could be the problem - cmd.Parameters.AddWithValue("@datAdmission", txtdatAdmission);
Make sure you pass a valid datetime format in this field.


需要在下面添加,以及Sunsara建议的

Need to add for below as well as Sunsara suggested

cmd.Parameters.AddWithValue("@datAdmission", txtdatAdmission);
           cmd.Parameters.AddWithValue("@strName", txtStudentName);


这篇关于使用C#插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆