Hi samsmuthu,
Refer the below sample.
HTML
<asp:Button Text="Read dat File" runat="server" ID="btnRead" OnClick="ReadDat" />
C#
using System.Data.SqlClient;
using System.IO;
protected void ReadDat(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(Server.MapPath("~/Files/test.dat"));
for (int i = 0; i < lines.Length; i++)
{
Int32 id = Convert.ToInt32(lines[i].Split(' ')[0]);
DateTime dateTime = Convert.ToDateTime(lines[i].Split(' ')[1].ToString() + " " + lines[i].Split(' ')[2].ToString());
int id1 = Convert.ToInt32(lines[i].Split(' ')[3]);
int id2 = Convert.ToInt32(lines[i].Split(' ')[4]);
int id3 = Convert.ToInt32(lines[i].Split(' ')[5]);
int id4 = Convert.ToInt32(lines[i].Split(' ')[6]);
Insert(id, dateTime, id1, id2, id3, id4);
}
}
public void Insert(int id, DateTime date, int id1, int id2, int id3, int id4)
{
string str = ConfigurationManager.ConnectionStrings[1].ConnectionString;
using (SqlConnection con = new SqlConnection(str))
{
string query = "INSERT INTO TableName VALUES(" + id + ",'" + date + "'," + id1 + "," + id2 + "," + id3 + "," + id4 + ")";
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Imports System.Data.SqlClient
Imports System.IO
Protected Sub ReadDat(sender As Object, e As EventArgs)
Dim lines As String() = File.ReadAllLines(Server.MapPath("~/Files/test.dat"))
For i As Integer = 0 To lines.Length - 1
Dim id As Int32 = Convert.ToInt32(lines(i).Split(" "c)(0))
Dim dateTime As DateTime = Convert.ToDateTime(lines(i).Split(" "c)(1).ToString() + " " + lines(i).Split(" "c)(2).ToString())
Dim id1 As Integer = Convert.ToInt32(lines(i).Split(" "c)(3))
Dim id2 As Integer = Convert.ToInt32(lines(i).Split(" "c)(4))
Dim id3 As Integer = Convert.ToInt32(lines(i).Split(" "c)(5))
Dim id4 As Integer = Convert.ToInt32(lines(i).Split(" "c)(6))
Insert(id, dateTime, id1, id2, id3, id4)
Next
End Sub
Public Sub Insert(id As Integer, dateTime As DateTime, id1 As Integer, id2 As Integer, id3 As Integer, id4 As Integer)
Dim str As String = ConfigurationManager.ConnectionStrings(1).ConnectionString
Using con As New SqlConnection(str)
Dim query As String = "INSERT INTO TableName VALUES(" & id & ",'" & dateTime & "'," & id1 & "," & id2 & "," & id3 & "," & id4 & ")"
Using cmd As New SqlCommand(query, con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub