Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
how to save video in database sql server using xamarin android web service and display in listview
I would strongly advise not to store videos in your database due to the following reasons:
1). Doing so would bloat your database size
2). Slow down the performance of the database
3). It opens your application to Denial of Service attacks (DDOS).
You should look forward to storing your videos on a file dedicated storage like:
1). Cloud Storage.
2). FTP storage.
3). Server's file system.
4). Etc...
If you really want to store videos on your database for testing or bench marking purposes, you can try the following code:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Portal.WebApp.Models
{
public class DbBlobSaver
{
#region Public Methods
public void Execute(byte[] fileBytes)
{
using (var dbConnection = new SqlConnection("Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI"))
{
var command = BuildCommand(dbConnection);
var blobParameter = BuildParameter(fileBytes);
command.Parameters.Add(blobParameter);
try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
// Log errors here
}
finally
{
command.Connection.Close();
}
}
}
#endregion
#region Private Methods
private SqlParameter BuildParameter(byte[] fileBytes)
{
var blobParameter = new SqlParameter("#BlobFile", SqlDbType.Binary);
blobParameter.Value = fileBytes;
return blobParameter;
}
private SqlCommand BuildCommand(SqlConnection connection)
{
var command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = "INSERT INTO BlobsTable(BlobFile)" + "VALUES (#BlobFile)";
return command;
}
#endregion
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have 2 forms in my program. I am using a SQL Server Compact 3.5 database file (.sdf) from C#
albums_tbl table has two columns: id, name
In form 1 when I use this code :
private void button1_Click(object sender, EventArgs e)
{
SqlCeCommand cm = new SqlCeCommand("INSERT INTO albums_tbl(album_name) VALUES (#album_name) ", cn);
cm.Parameters.AddWithValue("#album_name", textBox1.Text);
int affectedrows = cm.ExecuteNonQuery();
if (affectedrows > 0)
{
MessageBox.Show("insert shod !");
}
}
the program inserts well, but when I use the exact code in form 2 it errors this when I want to insert :
You don't open connection yet.
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')";
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
Refer: https://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection(v=vs.100).aspx
Hope this helps.
You have not opened a connection , you just simply need to do as follow
private void button1_Click(object sender, EventArgs e)
{
SqlCeConnection con=new SqlCnConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'");
SqlCeCommand cm = new SqlCeCommand("INSERT INTO albums_tbl(album_name) VALUES (#album_name) ", cn);
cm.Parameters.Add(new SqlCeParameter(#album_name, textBox1.Text));
con.Open();
cm.ExecuteNonQuery();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We are having some problems displaying the experience a player has from our database.
This is for a school project, would like some hints :)
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid");
string uID = Session["userID"].ToString();
CheckExp.Parameters.AddWithValue("#uid", uID);
try
{
connection.Open();
SqlDataReader ExpReader = null;
ExpReader = CheckExp.ExecuteReader();
if (ExpReader.Read())
{
Label6.Text = ExpReader["Experience"].ToString();
}
}
catch (Exception ex)
{
Label6.Text=(ex.Message);
}
finally
{
connection.Close();
}
You didn't connect your SqlConnection and SqlCommand.
Just define your connection as a second parameter like;
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid", connection);
Or you can assing your SqlCommand.Connection property like;
CheckExp.Connection = connection;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need to insert a record in Access 2000 database using C#. The code fails at the SqlConnection. Please help.
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Trading.mdb";
string commandText = "INSERT INTO Order (OpenDate) VALUES (#OpenDate)";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.AddWithValue("#OpenDate", DateTime.Now);
try
{
command.Connection.Open();
int response = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: {0}" + ex.Message);
}
}
Problem : you are working with MS-Access Database but using SqlServer objects.
Solution : you need to use OleDbConnection object instead of SqlConnection and OleDbCommand instead of SqlCommand
Try This:
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Trading.mdb";
string commandText = "INSERT INTO Order (OpenDate) VALUES (?)";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(commandText, connection);
command.Parameters.AddWithValue("#OpenDate", DateTime.Now);
try
{
command.Connection.Open();
int response = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: {0}" + ex.Message);
}
}
You need OleDbConnection not SqlConnection which is used for SQL Server.
See: Walkthrough: Editing an Access Database with ADO.NET
also: How to: Create Connections to Access Databases
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am developing a C# Windows application. I have used access database. When I selecting data from database I am getting data, but when inserting data it's not gets inserted and also it's not showing any error.
But when I run the same insert query in Access it gets inserted. Here is my code:
public void connCheck()
{
try
{
cn = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database\MyDatabase.mdb;Persist Security Info=True;Jet OLEDB:Database Password=2013");
if (cn.State == ConnectionState.Closed)
cn.Open();
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
public bool ExecuteNonQuery()
{
try
{
connCheck();
string sqlQuery = "INSERT INTO tblResult(ExamSet,SetId,FullMarks,ObtainedMarks,MarksPercentage,ElapsedTime,LastQIndex,CreatedDate,Completed)
Values(1,27,'200.00',0,0,0,1,DATE(),'N')";
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
cmd.CommandText = sqlQuery;
cmd.ExecuteNonQuery();
return true;
}
catch(OleDbException ex)
{
ErrorMsg = ex.ToString();
return false;
}
finally
{
cn.Close();
cn.Dispose();
cmd.Dispose();
}
}
You did not defined your cmd..
add this line
OledbCommand cmd=new OledbCommand();
How about replacing your query code like this
string sqlQuery = "INSERT INTO tblResult([ExamSet],[SetId],[FullMarks],[ObtainedMarks],[MarksPercentage],[ElapsedTime],[LastQIndex],[CreatedDate],[Completed])
Values(1,27,'200.00',0,0,0,1,DATE(),'N')";
Update:
One of the issues could be Security warning that disables the content.
Try and see if this works (Go to your MDB):
Click on 'External Data' tab
There should be a Security Warning that states "Certain content in the database has been disabled"
Click the 'Options' button
Select 'Enable this content' and click the OK button
Try parameterised queries via cmd.Parameters.Add or AddRange. Example
var cmd = new SqlCommand("INSERT INTO tbl_name (a, b, c) VALUES (#a, #b, #c)");
cmd.Parameters.AddRange(new[] { new SqlParameter("#a", field1), new SqlParameter("#b", field2), new SqlParameter("#c", field2) });
(Posted on behalf of the OP).
Thanks everyone for your help, I got the solution. Actually there is no problem in my code. Problem is that I have created database file in a folder. But when I build the project it created a duplicate database with same folder an file name in bin folder.
So every time it gets inserted in that database. And I was checking in the database file which I have created. So I thought it's not working.