Facing trouble while inserting bulk data into sql - sql-server

In MVC4 , i have following code in my view :
<script>
var things = [];
function fun () {
var Quran = {
"surah": things[1].surah,
"ayah": things[1].ayah,
"verse": things[1].verse
};
things.push(Quran);
for (var n = 0; n < length; n++) {
$.ajax({
contentType: 'application/json; charset=utf-8',
method: 'GET',
url: "Gateway/DB_Rola?action=1",
data: things[n],
success: function (Data) {
var mera_obj = Data.key;
document.getElementById("Param2").value = '(' + mera_obj.Response_Code + ' , ' + mera_obj.Response_Description + ')';
},
error: function () {
alert("ERROR: can't connect to Server this time");
return false; }
});
alert("done for "+(n+1));
} // loop ends
return false;
}; // function ends
and controller method is :
public ActionResult DB_Rola(thing things)
{
string connectionString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\PROGRAM FILES (X86)\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\PEACE_QURAN.MDF;Integrated Security=True";
System.Data.SqlClient.SqlConnection connection = new SqlConnection(connectionString);
int surah = things.surah;
int ayah =things.ayah;
String verse = things.verse;
// designing parametiric_query from the parameters
string query = "insert into Ayyat_Translation_Language_old_20131209 values(null,null,#Surah,#Verse)";
SqlCommand cmd = new SqlCommand(query, connection);
connection.Open();
//setting parameters for parametric query
SqlParameter Parm1 = new SqlParameter("Surah", surah);
SqlParameter Parm2 = new SqlParameter("Ayah", ayah);
SqlParameter Parm3 = new SqlParameter("Verse", verse);
//adding parameters
cmd.Parameters.Add(Parm1);
cmd.Parameters.Add(Parm2);
cmd.Parameters.Add(Parm3);
cmd.ExecuteNonQuery();
System.IO.StreamWriter file = new System.IO.StreamWriter(#"E:\Office_Work\Peace_Quran\Peace_Quran\Files\hisaab.txt", true);
file.WriteLine(" "+things.ayah);
file.Close();
connection.Close();
return View();
}
as mentioned above in code ,there is loop in my view page that passes single object at a time which is received in above controller method .it works for small amount of data but when i send bulk .i.e. 50+ records at once, some of records are not saved in my DB. i don't know what's wrong with my DB code. please help me figure it out.

Related

Angular 11 HTTP POST to SQL Server Database

I am trying to post my inputs to my SQL Server database. I can in fact POST to the database, but I get back a blank response. I know it's because I am returning "Success" instead of my variables but how to I correctly format that for the return statement?
POST method:
[HttpPost]
public JsonResult Post(Weather Wea)
{
string query = #"INSERT INTO dbo.Information (Date, TemperatureC, TemperatureF, Summary) VALUES ('" + Wea.Date + #"'
,'" + Wea.TemperatureC + #"'
,'" + Wea.TemperatureF + #"'
,'" + Wea.Summary + #"'
)";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("WeatherAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Success");
}
Front-end POST
export class PostDataComponent {
baseUrl: string;
date: number;
temperatureC: number;
summary: string;
weatherForm: FormGroup;
constructor(public http: HttpClient, #Inject('BASE_URL') baseUrl: string, private formBuilder: FormBuilder) {
this.baseUrl = "https://localhost:44347/WeatherForecast";
this.weatherForm = formBuilder.group({
Date: new FormControl(),
TemperatureC: new FormControl(),
Summary: new FormControl()
});
}
CreateData() {
const params = new HttpParams({
fromObject: {
'date': this.weatherForm.value.Date.toString(),
'temperatureC': this.weatherForm.value.TemperatureC.toString(),
'summary': this.weatherForm.value.Summary.toString()
}
});
console.log(params);
this.http.post(this.baseUrl, {},{ params: params }).subscribe(data => {
console.log(data);
});
}
}
Couple things here.
As marc_s commented, you should be using parameterization instead of concatenating to avoid any potential SQL injection:
string query = #"INSERT INTO dbo.Information (Date, TemperatureC, TemperatureF, Summary) VALUES (#Date, #TemperatureC, #TemperatureF, #Summary)";
...
using (System.Data.SqlClient.SqlCommand myCommand = new SqlCommand(query, myCon))
{
myCommand.Parameters.AddWithValue("#Date", Wea.Date);
myCommand.Parameters.AddWithValue("#TemperatureC", Wea.TemperatureC);
myCommand.Parameters.AddWithValue("#TemperatureF", Wea.TemperatureF);
myCommand.Parameters.AddWithValue("#Summary", Wea.Summary);
...
Unless you have a trigger on your target table with an output, your query isn't returning any data (just the number of rows inserted) and your SqlDataReader is empty. You could get rid of the reader/DataTable and use myCommand.ExecuteScalar() instead in this case. If you do have a trigger outputting the inserted data, disregard this.
If you don't have an output trigger but do still need to return the inserted values for whatever reason, you could keep your SqlDataReader and update your query to the following
string query = #"INSERT INTO dbo.Information (Date, TemperatureC, TemperatureF, Summary)
OUTPUT inserted.Date,inserted.TemperatureC,inserted.TemperatureF,inserted.Summary
VALUES (#Date, #TemperatureC, #TempreatureF, #Summary)";
Without knowing the response format you're looking for, it's hard to give an answer on how to generate it. If you need to return the inserted values, you could use the OUTPUT keyword as in the previous bullet and serialize your DataTable.

Dapper What is wrong?

I have this code should be working but this is throwing an exception Must declare variable scalar
public async Task<Job> GetJobByUd(Guid jobId)
{
var sql = "select * from dbo.Jobs where JobId = #jobId;";
using (DbConnection db = new SqlConnection(_connectionString))
{
var job = await db.QuerySingleOrDefaultAsync<Job>(sql, new {jobId});
return job;
}
}
Try like this, it should work.
public async Task<Job> GetJobByUd(Guid jobId)
{
var sql = "select * from dbo.Jobs where JobId = #jobId;";
using (DbConnection db = new SqlConnection(_connectionString))
{
var job = await db.QueryAsync<Job>(sql, new {jobId = jobId}).FirstOrDefault();
return job;
}
}

$resource POST Error 400

Hi guys i'm working with angular $resource to make POST call. This is my FE function
$scope.showPrompt = function (ev) {
$scope.usernameSelected = [];
$scope.users.forEach(function (element) {
if (element.checked)
$scope.usernameSelected.push(element);
console.log($scope.usernameSelected);
});
var conferma = $mdDialog.prompt()
.title('Insert group name?')
.clickOutsideToClose(true)
//.textContent('Bowser is a common name.')
.placeholder('Group name')
.ariaLabel('Group name')
.targetEvent(ev)
.ok('Crea gruppo')
.cancel('Annulla');
$mdDialog.show(conferma).then(function (result) {
GroupService.group({}, {
creatore: $cookieStore.get('username'),
gruppo: result,
partecipanti: $scope.usernameSelected},
function (data) {
console.log("GRUPPO", data);
});
$scope.status = 'NOMEGRUPPO: ' + result + '.'; //inserito il nome gruppo
}, function () {
$scope.status = 'INSERT NOME GRUPPO';
});
};
This the GroupService:
var groupService = angular.module("groupService",['ngResource']);
groupService.factory("GroupService",['$resource',
function($resource){
var url = "";
return $resource("",{},{
group: {method : "POST", url:url+"group", isArray: false},
findGroup: {method : "GET", url:url+"findGroup", isArray: true}
});
}]);
I have this error:
angular.js:14328 Possibly unhandled rejection: {"data":"<!DOCTYPE html><html><head><title>Apache Tomcat/8.5.11 - Error report</title><style type=\"text/css\">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style> </head><body><h1>HTTP Status 400 - </h1><div class=\"line\"></div><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><hr class=\"line\"><h3>Apache Tomcat/8.5.11</h3></body></html>","status":400,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"group","data":{"creatore":"ciro","gruppo":"nuovo","partecipanti":[{"id":0,"username":"a","email":"a#aa","password":null,"name":"a","surname":"a","longitude":0,"latitude":0,"checked":true},{"id":0,"username":"ciao","email":"ciao#ciao","password":null,"name":"ciao","surname":"ciao","longitude":0,"latitude":0,"checked":true}]},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":""}
This is my BE code. I have a spring controller that manage client requests
#RequestMapping(value = "/group", method = RequestMethod.POST)
public #ResponseBody
Group createGroup(#RequestBody Group json, HttpServletRequest request) throws SQLException, ClassNotFoundException {
Group g = DBUtils.insertGroup(json.getPartecipanti(), json.getGruppo(), json.getCreatore());
return g;
}
This is my DBUtils.insertGroup
public static Group insertGroup(List<String> members, String groupName, String userCreator) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn;
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "insert into gruppo \n"
+ " (idCreatorUser,groupName,idPartecipante,data) values (?,?,?,?) ";
PreparedStatement pstm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
java.util.Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
Utente creator = DBUtils.findUser(userCreator);
pstm.setInt(1, creator.getId());
pstm.setString(2, groupName);
pstm.setInt(3, creator.getId());
pstm.setTimestamp(4, timestamp);
int res = pstm.executeUpdate();
ResultSet rsId = pstm.getGeneratedKeys();
rsId.next();
int idGruppo = rsId.getInt(1);
Group response = new Group();
response.setIdGruppo(idGruppo);
response.setGruppo(groupName);
response.setIdCreatore(creator.getId());
response.setData(timestamp);
response.setPartecipanti(members);
response.setCreatore(creator.getUsername());
System.out.println(response);
sql = "insert ignore into gruppo \n"
+ " (idGruppo,idCreatorUser,groupName,idPartecipante,data) values (?,?,?,?,?) ";
pstm = conn.prepareStatement(sql);
Utente part;
for (int i = 0; i < members.size(); i++) {
pstm.setInt(1, idGruppo);
pstm.setInt(2, creator.getId());
pstm.setString(3, groupName);
part = DBUtils.findUser(members.get(i));
pstm.setInt(4, part.getId());
pstm.setTimestamp(5, timestamp);
pstm.executeUpdate();
}
conn.close();
return response;
}
I want to send in body some parameters but I can't to figure out what is the syntact error made.

SQL Server backup failed for server xxxx

I'm trying to take a backup of a local SQL Server database to my local machine through c# code and I am getting Backup failed for server .
Code as follows:
using (var con = new SqlConnection(conn))
{
const string destination = "d:\\backups"+"/";
var sqlServer = new Server(new ServerConnection(con));
con.Open();
var bkpDatabase = new Backup { Action = BackupActionType.Database, Database = name };
var bkpDevice = new BackupDeviceItem(destination + name + ".bak", DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.Checksum = true;
bkpDatabase.ContinueAfterError = true;
bkpDatabase.Incremental = false;
bkpDatabase.Initialize = true;
// Perform the backup
bkpDatabase.SqlBackup(sqlServer);
con.Close();
}
I have used the following references :
mircrosoft.sqlserver.smo;
mircrosoft.sqlserver.smoextended;
mircrosoft.sqlserver.management.sdk.sfc;
mircrosoft.sqlserver.connectioninfo;
Inner exception :
Microsoft.SqlServer.Management.Smo.FailedOperationException was caught
HelpLink=http://go.microsoft.com/fwlink? ProdName=Microsoft+SQL+Server&ProdVer=10.50.2500.0+((KJ_PCU_Main).110617- 0026+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptio nText&EvtID=Backup+Server&LinkId=20476
HResult=-2146233088
Message=Backup failed for Server 'XXXXX'.
Source=Microsoft.SqlServer.SmoExtended
Operation=Backup
StackTrace:
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
at DatabaseCreation.Program.BackupDatabase(String name, String conn) in c:\Users\Documents\Visual Studio 2013\Projects\Databaseinstallation\DatabaseCreation\Program.cs:line 154
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.SqlServer.ConnectionInfo
StackTrace:
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_IsOpen()
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.PoolConnect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_DatabaseEngineType()
at Microsoft.SqlServer.Management.Smo.Server.GetExecutionManager()
at Microsoft.SqlServer.Management.Smo.Server.get_ExecutionManager()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.Backup.Script(Server targetServer)
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
Maybe you have to escape the \ character in your destination path:
#"d:\backups"
or
"d:\\backups"
Try the following:
using (var con = new SqlConnection(conn))
{
const string destination = "c:\\backups";
var fileName = Path.Combine(destination, String.Format("{0}.bak", name));
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
var sqlServer = new Server(new ServerConnection(con));
var bkpDatabase = new Backup
{
Action = BackupActionType.Database,
Database = name
};
var bkpDevice = new BackupDeviceItem(fileName, DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.Checksum = true;
bkpDatabase.ContinueAfterError = true;
bkpDatabase.Incremental = false;
bkpDatabase.Initialize = true;
bkpDatabase.SqlBackup(sqlServer);
}

Couldn't send and store String data to database through WCF

I have created a Windows 8 app, I have a table in SQL server database to store people's name, " [Name] VARCHAR (50)"
I have manage to send and save integer values to database, but when i modified my coding to store the string, it does not work, table data is empty. Please help!
itemDetail.html
<div>
<input id="join1" type="text" />
<button id="joinbtn">insert</button>
</div>
itemDetail.js
var joinButton = document.getElementById('joinbtn');
// Register Click event
joinButton.addEventListener("click", joinButtonClick, false);
function joinButtonClick() {
// Retrieve element
var baseURI2 = "http://localhost:45573/AddService.svc/Join";
var jointext = document.getElementById('join1').value;
WinJS.xhr({
type: "POST",
url: baseURI2,
headers: { "Content-type": "application/json" },
data: '{"namet":' + jointext + '}'
}).then(function complete(request) {
var resdata = request.responseText;
}, function error(er) {
var err = er.statusText;
})
}
AddService.svc.cs
public void Join(string namet)
{
string connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["Database1ConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
string sql = "INSERT INTO Table2(Name) VALUES (#Name)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("#Name", namet);
try
{
con.Open();
int numAff = cmd.ExecuteNonQuery();
}
con.Close();
}
IAddService.cs
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
void Join(string namet);
Thank you!
I think the problem may be in this line
data: '{"namet":' + jointext + '}'
Try changing it to
data: '{"namet":\'' + jointext + '\'}'

Resources