Hi..
I want to create a seperate DataAccessLayer and do the inserting,updating...etc to a database.Actually I'm new to .net things.
I did it as below.But the database is not updated at all..
This is the DataAccessLayer class...
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ClassTest
{
class DataAccessLayer
{
public SqlConnection cn;
public SqlCommand dataCmd;
public SqlDataAdapter da;
public DataTable dt;
public String fullsql;
public DataAccessLayer()
{
}
public DataTable getTable(String sql)
{
cn = new SqlConnection("Data Source=MICROSOFTSL-PC;Initial Catalog=Test;User ID=sa;Password=sa");
dataCmd = new SqlCommand();
cn.Open();
dt = new DataTable();
dataCmd.CommandText = sql;
da = new SqlDataAdapter(dataCmd);
da.SelectCommand = new SqlCommand(sql, cn);
da.Fill(dt);
cn.Close();
return dt;
}
public void updateTable(DataTable table)
{
try
{
getTable(fullsql);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
table = dt;
da.Update(table);
}
catch (Exception ex)
{
String error=ex.Message;
}
}
}
}
This is the form1.cs where I want to instantiate the DAL..
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ClassTest
{
class DataAccessLayer
{
public SqlConnection cn;
public SqlCommand dataCmd;
public SqlDataAdapter da;
public DataTable dt;
public String fullsql;
public DataAccessLayer()
{
}
public DataTable getTable(String sql)
{
cn = new SqlConnection("Data Source=MICROSOFTSL-PC;Initial Catalog=Test;User ID=sa;Password=sa");
dataCmd = new SqlCommand();
cn.Open();
dt = new DataTable();
dataCmd.CommandText = sql;
da = new SqlDataAdapter(dataCmd);
da.SelectCommand = new SqlCommand(sql, cn);
da.Fill(dt);
cn.Close();
return dt;
}
public void updateTable(DataTable table)
{
try
{
getTable(fullsql);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
table = dt;
da.Update(table);
}
catch (Exception ex)
{
String error=ex.Message;
}
}
}
}
I can't think of why this doesn't store the values..
Plz help me.
Thank you so much..