如何在数据库中存储值 [英] How to store values in database
本文介绍了如何在数据库中存储值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolManagement
{
class StudentManager
{
public string ConnectionString { get { return @"Data Source=GANESH_DIEHARD\SqlExpress;Initial Catalog=SchoolDB;Integrated Security=True;"; } }
public void ManageStudents(int id, string name, int Class, int Branch, string Address, DateTime dateofbirth, bool delete)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("ManageStudents", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
if (id > 0)
{
cmd.Parameters.Add(new SqlParameter("@id", id));
}
if (!string.IsNullOrEmpty(name))
{
cmd.Parameters.Add(new SqlParameter("@name", name));
}
////if (!string.IsNullOrEmpty(Class))
////{
cmd.Parameters.Add(new SqlParameter("@Class", Class));
//}
//if (!string.IsNullOrEmpty(Branch))
//{
cmd.Parameters.Add(new SqlParameter("@Branch", Branch));
//}
if (!string.IsNullOrEmpty(Address))
{
cmd.Parameters.Add(new SqlParameter("@Address", Address));
}
//if (!string.IsNullOrEmpty(dateofbirth))
{
cmd.Parameters.Add(new SqlParameter("@dateofbirht", dateofbirth));
}
if (delete)
{
cmd.Parameters.Add(new SqlParameter("@delete", delete));
con.Open();
var result = cmd.ExecuteNonQuery();
con.Close();
}
}
public DataTable GetStudents(int id, string name, string Class, string Branch)
{
DataTable dtStudents = new DataTable();
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("GetStudents", con);
cmd.CommandType = CommandType.StoredProcedure;
if (id > 0)
{
cmd.Parameters.Add(new SqlParameter("@id", id));
}
if (!string.IsNullOrEmpty(name))
{
cmd.Parameters.Add(new SqlParameter("@name", name));
}
if (!string.IsNullOrEmpty(Class))
{
cmd.Parameters.Add(new SqlParameter("@Class", Class));
}
if (!string.IsNullOrEmpty(Branch))
{
cmd.Parameters.Add(new SqlParameter("@Branch", Branch));
}
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dtStudents);
return dtStudents;
}
}
}
推荐答案
Hi
如果您使用的是storedprocedure,那么您可以使用以下代码将数据插入数据库。
Hi
If you are using storedprocedure then you can use following code to insert the data into database.
//Code Behind file code of web form
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
Class1 cs = new Class1();
object[] o = new object[4];
o[0] = txtName.Text.Trim();
o[1] = txtAdd.Text.Trim();
o[2] = Convert.ToDateTime(txtbdt.Text.Trim());
o[3] = txtsal.Text.Trim();
cs.InsertData(o, "InsertInformation");
Response.Write("<script>alert('Data Inserted Successfully!')</script>");
}
catch (Exception ex)
{
Response.Write(ex);
}
}
// Class File code which interacts both web form and stored Procedure
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Dal
/// </summary>
public class Dal
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString.ToString());
SqlDataAdapter da;
SqlCommand cmd;
DataSet ds;
public Dal()
{
//
// TODO: Add constructor logic here
//
}public void InsertData(object[] o,string str)
{
try
{
// Get Length of Object pass from View Page
int a = o.Length;
// Create Object to get store procedure parameters
object[] abc = new object[a];
// Check Connection Open or Close
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// Start Code to get SQL parameter from Stored Procedure
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = con;
myCommand.CommandText = str;
myCommand.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(myCommand);
for (int i = 0; i < myCommand.Parameters.Count - 1; i++)
{
abc[i] = myCommand.Parameters[i + 1].ParameterName.ToString();
}
// End code to get SQL parameter from Stored Procedure
// Start Code to Insert data into table using Stored Procedure
cmd = new SqlCommand(str, con);
for (int i = 0; i < o.Length; i++)
{
SqlParameter sp = new SqlParameter();
sp.ParameterName = abc[i].ToString();
sp.Value = o[i];
cmd.Parameters.Add(sp);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
//End Code to Insert data intot table using stored procedure
}
catch (Exception ex)
{
throw ex;
}
}
}
//Stored Procedure by which we can insert data into database
ALTER PROCEDURE dbo.InsertInformation
@Name nvarchar(50),
@Address nvarchar(max),
@Bdt date,
@sal nvarchar(50)
AS
insert into Information (IName,IAddress,Birthdate,Salary) values(@Name,@Address,@Bdt,@sal)
RETURN
希望这对你有所帮助。
谢谢
hope this will help you.
thanks
你也可以使用一种方法你可以编写代码来插入数据。
You can also use one method where you can make code to insert the data.
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "AddEmployee";
cmd.Parameters.Add("@FirstName",SqlDbType.VarChar).Value = txtFirstName.Text.Trim();
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text.Trim();
cmd.Parameters.Add("@BirthDate", SqlDbType.DateTime).Value = txtBirthDate.Text.Trim();
cmd.Parameters.Add("@City", SqlDbType.VarChar).Value =
txtCity.Text.Trim();
cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim();
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
lblMessage.Text = "Record inserted successfully";
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
希望这对你也有帮助。
谢谢
hope this will also help you.
thanks
这篇关于如何在数据库中存储值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文