如何创建连接字符串 [英] how to create Connection string

查看:88
本文介绍了如何创建连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击我在asp.net应用程序上创建的插入按钮时,我自动输入的所有记录都存储在我的employee_tab(表格)中。插入按钮的代码如下:

< pre lang =c#> SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
com.Connection = con;
con.ConnectionString =( @ add name = constr ; connectionString = Data Source = localhost; Initial Catalog = master; Integrated Security = True );
int a = Convert.ToInt32(txtEmpId.Text);
string b =(txtEmp_name.Text);
int c = Convert.ToInt32(txtSalary.Text);
string d =(txtCity.Text);
com.CommandText =
INSERT INTO employee_tab(EmpId,Emp_name,Salary,City )值(@ a,@ b,@ c,@ d) ;
com.Parameters.AddWithValue (
@a ,a);
com.Parameters.AddWithValue(
@b ,b);
com.Parameters.AddWithValue(
@c ,c);
com.Parameters.AddWithValue(
@d ,d);
com.Connection.Open();
com.ExecuteNonQuery();
com.Connection.Close();



我也创建了一个SP,它也给出了错误。我想这样做仅在SP的帮助下。

存储过程如下:

  create   procedure  spdatabaseconnectivity 

@ EmpId int
@ Emp_name
@ Salary
@ City
as
begin
insert into employee_tab(EmpId,Emp_name,薪水,城市)
@ EmpId @Emp_name @ Salary @ city ))
end



我的连接字符串是否正确?

帮我解决这个que.my表驻留在系统数据库中。我认为我的连接字符串有问题。我在执行c#代码时遇到的错误是:每个配置文件只允许一个元素,如果存在,则必须是根元素的第一个子元素。

谢谢。

解决方案

除非表驻留在master数据库中。您的表位于哪个数据库中?您得到的错误是什么?


您好,



要创建连接字符串,请查看以下链接:



http://www.connectionstrings.com/ [ ^ ]





并在修改连接字符串后,使用存储过程插入数据:



示例: insert-record-in-database-using-stored.html





请查看解决方案..


when i click insert button that i have created on asp.net application all records that i enter automatically gets stored in my employee_tab(table).my code for insert button is as follows:

SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
com.Connection = con;
con.ConnectionString = (@"add name="constr"; connectionString ="Data Source=localhost;Initial Catalog=master;Integrated Security=True");
    int a = Convert.ToInt32(txtEmpId.Text);
    string  b = (txtEmp_name.Text );
int c = Convert.ToInt32(txtSalary.Text);
string  d = (txtCity.Text);
com.CommandText = "INSERT INTO employee_tab(EmpId,Emp_name,Salary,City) values (@a,@b,@c,@d)";
com.Parameters.AddWithValue("@a" , a );
com.Parameters.AddWithValue("@b",b );
com.Parameters.AddWithValue("@c", c );
com.Parameters.AddWithValue("@d",d );
com.Connection.Open();
com.ExecuteNonQuery();
com.Connection.Close();


Also i have created a SP and it is also giving error.i want to do this all with help of SP only.
stored proc is as follows:

create procedure spdatabaseconnectivity
( 
    @EmpId int,
    @Emp_name,
    @Salary,
    @City)
as
begin
    (insert into employee_tab(EmpId,Emp_name,Salary,City)
    values (@EmpId,@Emp_name,@Salary,@city))
end


is my connection string is correct?
help me solve this que.my table resides in system database.i think there is a problem with my connection string.the error i am getting when executing c# code is : Only one element allowed per config file and if present must be the first child of the root element.
Thanks.

解决方案

Not unless the table resides in master database. Which database does your table reside in? What is the error you are getting?


Hi,

For creating connection string plz look into the following link :

http://www.connectionstrings.com/[^]


and after modifying your connections string , insert data using stored procedure :

Example : insert-record-in-database-using-stored.html


Please have a look into the solution..


这篇关于如何创建连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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